Search This Blog

Wednesday, January 15, 2014

Creating a List Item instance programmatically using the object model


Creating a List Item instance programmatically using the object model


Adding a List Item to a Custom List and Get Item ID , Update the list item by Id and delete Item


Using(SPSite sSite = new SPSite(SPContext.Current.web.url)
{
Using(SPWeb sWeb = sSite.OpenWeb())
{
    sWeb.AllowUnsafeUpdates=true;
    SPList sList = sWeb.Lists["PolicyDetails"];
    //Add new item in the list
    SPListItem sItem = sList.Item.Add();
    sItem["Title"] = "Hello Policy Details";
    sItem["Description"] = "Policy Details Description"
    sItem.Update();
    //Get Item Id
    listItemId = Item.ID;
    //Update The List Item By ID
    SPListItem sItem = sList.GetItemById(listItemId);
    sItem["Description"] = "Update Description";
    sItem.Update();
    //Delete Item
    SPListItem iItem = iList.GetItemByID(listItemId);
    Item.Delete();
    sWeb.AllowUnsafeUpdates=false;
}
}

Reference:
https://www.nothingbutsharepoint.com/sites/devwiki/sp2007dev/pages/creating%20a%20list%20item%20instance%20programmatically%20using%20the%20object%20model.aspx

No comments:

Post a Comment