Thursday, February 04, 2010

Method to get Sharepoint Column Internal Names

#region Method to get the Internal Name for the Column in the List
public static string GetColumnInternalName(SPList List, string ActualColumnName)
{
string ColumnInternalName = string.Empty;
for (int fc = 0; fc < List.Fields.Count; fc++)
{
if (List.Fields[fc].Title == ActualColumnName)
{
//Get Internal Name for the displayed column name
ColumnInternalName = List.Fields[fc].InternalName;
return ColumnInternalName;
}
}
return ColumnInternalName;

}
#endregion

Getting the Field Values of Infopath Form stored in Sharepoint Form Library

//Get the Site
SPSite site= new SPSite(SITE_URL);
SPWeb Web = site.OpenWeb();
SPFile File = Web.GetFile(InfoPathDocUrl);

//Load the file into XML Document
XmlDocument Document = new XmlDocument();
Document.Load(File.OpenBinaryStream());

//Get the Root node and NamespaceManager of the Infopath form
XmlElement root = Document.DocumentElement;
XmlNamespaceManager NamespaceManager = InitNamespaceManager(Document);

//Get the Field values
string Field1Value= root.SelectSingleNode(Field1XPath, NamespaceManager).InnerText;
string Field2Value= root.SelectSingleNode(Field2XPath, NamespaceManager).InnerText;

Monday, February 01, 2010

How to update Infopath form fields Programatically?

public static void UpdateInfopathFormData(SPFile File, string NodeXPath, string UpdateValue,string NameSpace)
{
XmlDocument Document = new XmlDocument();
Document.Load(File.OpenBinaryStream()); // Get the FDRY document file into an XML Document

XPathNavigator root = Document.CreateNavigator();
XmlNamespaceManager nsm = new XmlNamespaceManager(Document.NameTable);
nsm.AddNamespace("my", NameSpace);//Get the NameSpace from infopath form(Open Infopath form in design mode. Click on datasource from design tasks.Click on any field. Go to properties. Go to Detalis Tab. Copy the "Namespace" value).

SetValueSpecial(root, NodeXPath, UpdateValue, nsm);
Byte[] data = Encoding.UTF8.GetBytes(Document.OuterXml);
File.SaveBinary(data);
}

public static void SetValueSpecial(XPathNavigator Root, string path, string value, XmlNamespaceManager NamespaceManager)
{

XPathNavigator node = Root.SelectSingleNode(path, NamespaceManager);

if (node.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance"))
{
node.DeleteSelf();
}
node.SetValue(value);
}

How to display hand icon on mouse over of Infopath Button?

1. Open the Infopath Form in Design mode.
2. Click on "Save as Source Files..." from File menu.
3. Go to the folder where Source files are stored.
4. Open the xsl file for which you want to display hand icon on buttons.(Note that it ill create an xsl file for each view of the infopath form).

5. Search for the button with "type="button" in search criteria
6. For that button style add "CURSOR: pointer;".
Ex:
input class="langFont" title="" style="BORDER-RIGHT: #cbd8eb 1pt solid; BORDER-TOP: #000000 1pt; FONT-WEIGHT: bold; FONT-SIZE: xx-small; BORDER-LEFT: #cbd8eb 1pt solid; WIDTH: 82px; COLOR: #ffffff; BORDER-BOTTOM: #000000 1pt; HEIGHT: 17px; BACKGROUND-COLOR: #00003c; CURSOR: pointer;" type="button" size="1" value="Close" xd:CtrlId="BtnClose" xd:xctname="Button"

7. Save the file.
8. Next publish the manifest file(manifest.xsf) to the Sharepoint library.
9. Now if you open the infopath file and mouse over the button, it will display hand icon instead of pointer.

Wednesday, November 04, 2009

Programatically checking if the file exists in the specified location in Sharepoint

SPSite Site = new SPSite(SITE_URL);
SPWeb Web = Site.OpenWeb();
Bool isFileExists;
if(Web.GetFile(FilePath).Exists)
isFileExists = true;
else
isFileExists = false;

Method for converting a number to base in C#.net

public static string ConvertToBase(int num, int nbase)
{
String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

// check if we can convert to another base
if (nbase <> chars.Length)
return "";

int r;
String newNumber = "";

// in r we have the offset of the char that was converted to the new base
while (num >= nbase)
{
r = num % nbase;
newNumber = chars[r] + newNumber;
num = num / nbase;
}
// the last number to convert
newNumber = chars[num] + newNumber;

return newNumber;
}

Regular Expression for Validating Email

^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$