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})(\]?)$

How to Clone List in C#.net

To Copy List1 to List 2;

List List1;
//Create an array of strings
string[] arrStrList=new string[List1.Count];
//Copy the List to array of strings
List1.CopyTo(arrStrList);
//Define a new list
List List2= new List(List1.Count);
//Add array of strings to new list
List2.AddRange(arrStrList);

How to read and update LookUp Field in Sharepoint

//To Read LookUp Field
public static string ReadLookupValue(SPListItem item, string columnName)
{
SPFieldLookupValue lookupField = new SPFieldLookupValue(item[columnName].ToString());
return lookupField.LookupValue;
}

//To Update LookUp Field

int ListItemID = GetListItemID("ListWithLookUpColumn","ValueToBeUpdate");
ListItem[LookUpFieldColumnName] = ListItemID;
ListItem.Update();

/////

public static int GetListItemID(string ListName, string ListColumnValue)
{
int ListItemID=0;
try
{
SPSite Site = new SPSite(SITE_URL);
SPWeb Web = Site.OpenWeb();
SPList List = Web.Lists[ListName];
SPListItemCollection ListItems = List.Items;

int ListItemCount = ListItems.Count;
for (int i = 0; i < ListItemCount; i++)
{
SPListItem ListItem = ListItems[i];
if (ListItem["Title"].ToString() == ListColumnValue)
{
ListItemID =Convert.ToInt32(ListItem["ID"].ToString());
break;
}

}
return ListItemID;
}
catch (Exception ex)
{
return 0;
}
}

Programatically Sending Mails only to Outlook users and not sending to out side emails like Gmail,Yahoo etc.

I faced a problem in sending mails to outside of our domain.
I figured it out that i missed to add the following line of code before sending mail.

SmtpMail.SmtpServer = "EmailServername".

How to dislay Hand Icon for Buttons on mouse over in Infopath Forms

To display the Hand icon instead of pointer on mouse over of Button in Infopath Form, follow the steps below:

1) Open the Infopath form(InfopathForm.xsn) in design mode.

2) Save the form as Source Files ( Go to File menu, click on "Save As Source Files...)

3) Go to the saved Source Files location and open the xsl file(notice that it will create an xsl file for each view in the Infopath form) in an editor.

4) Search for "type="button"" and for that button style add "cursor:pointer". Save the file

5) Open "manifest.xsf" file in design mode from the same location.

6) Publish the manifest.xsf file.

7) Upload the file to Sharepoint Site.

8) Open the form in Sharepoint site and check if the buhand icon is displayed on button mouse over.