1

What would be a simple example of Updating an existing SPListItem?

2

You can use the following code

using(SPSite oSite = new SPSite("site url"))
{
    using(SPWeb oWebsite = oSite.OpenWeb())
    {
        SPList oList = oWebsite.Lists["Tasks"];
        SPListItem oListItem = oList.Items[5];
        oListItem["Title"] = "Some Title";
        oListItem.Update();// without this line item will not update
    }
}

Read more here: http://sharepoint.infoyen.com/2012/03/27/updating-a-list-item-programmatically/

1

Use this code

using (SPSite oSPsite = new SPSite("http://website url/"))
{
using (SPWeb oSPWeb = oSPsite.OpenWeb())
      {
        //Allow unsafe updates
            oSPWeb.AllowUnsafeUpdates = true;

        // Fetch the List
            SPList list = oSPWeb.Lists["MyList"];

        // Get the Item ID
            SPListItem itemToUpdate = list.Items[1];
            itemToUpdate["Description"] = "Changed Description";
            itemToUpdate.Update();
        oSPWeb.AllowUnsafeUpdates = false;
       }
}
0

I believe this would be SP 2013 best practice:

using (SPWeb web = SPContext.Current.Site.OpenWeb())
{
    SPList list = web.Lists.TryGetList("My Events");
    if (list != null)
    {
       SPListItem item = list.GetItemById(5);
       item["Title"] = "Some Title";
       item.Update();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.