Question

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

Était-ce utile?

La solution 3

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();
    }
}

Autres conseils

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/

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;
       }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top