public void GetAndUpdateList(string ListName, SPWeb site)
{
// Get List
SPList oSPList = site.GetList(ListName);
// Update List
SPListItem newItem = oSPList.Items.Add();
newItem["Title"] = "New List Added.";
newItem["Body"] = "This is exciting";
newItem["Expires"] = DateTime.Now + TimeSpan.FromDays(1);
newItem.Update();
// Also you might want to check for the Field Type before updating some times.
foreach(SPField field in newItem.Fields)
{
if(!field.Hidden && !field.ReadOnlyField)
{
// DO SOME THING
}
}
}
The Update method of the SPListItem object commits the changes to the list. If you don’t call the Update method, the list item data will not be saved.
GetList is the preferred method to access a list by a URL. GetListFromUrl and GetListFromWebPartPageUrl function the same way as GetList but throw a generic SPException on failure rather than the more descriptive FileNotFoundException.
Comments