Thursday, June 4, 2009

Rename title field of list programatically in Sharepoint


//Get the site
SPWeb web = SPContext.Current.Web;

// Get the list
SPList list = web.Lists["List Name"];
web.AllowUnsafeUpdates = true;

list.Fields["Title"].Title = "New Title";
list.Fields["Title"].Description = "This is description of field.";
list.Fields["Title"].Update(); //Don't forget to update list field

//Update list
list.Update();
web.AllowUnsafeUpdates = false;

Create a list programatically in sharepoint

Hi friends
Here i will show u how to create list of type Custom List.


SPWeb web = SPContext.Current.Web; //Get current site
string listName = "My New List";

try
{
//We r purposely putting it in try/catch block because if list not present in the site then code will throw an exception not return null.
SPList oldList = web.Lists[listName]; // Check if list is exist
}
catch (ArgumentException ex)
{
web.AllowUnsafeUpdates = true;
// Create a list
Guid listGuid = web.Lists.Add(listName, "This is my first custom list.", SPListTemplateType.GenericList);

// Set list properties
SPList list = web.Lists[listGuid];
list.OnQuickLaunch = true;

web.AllowUnsafeUpdates = true;
}

You can create any type of list by changing list template (i.e- SPListTemplateType.GenericList ).