SPListItem Update Example
-
09-10-2020 - |
質問
What would be a simple example of Updating an existing SPListItem?
解決 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();
}
}
他のヒント
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;
}
}
所属していません sharepoint.stackexchange