item.SystemUpdate() - It is used when we do not want fields such as Modified,ModifiedBy gets updated or if we do not want a new version should be created.
In the below code I used to get SharePoint List Item from different method and then update it.
- using (SPSite site = new SPSite(SPContext.Current.Site.ID))
-
- {
-
- using (SPWeb web = site.OpenWeb())
-
- {
-
- SPListItem item = GetItem("TestListSite1", 1);
-
- web.AllowUnsafeUpdates = true;
-
- item["Title"] = "Update List Item";
-
- item.SystemUpdate();
-
- web.AllowUnsafeUpdates = false;
-
-
-
- }
-
- }
Executing the above code gave an error below.
After searching I found that we should make AllowUnsafeUpdates = true of the item that we are updating and since I was getting item from a different method I used the below code.
The only change I made is this item.Web.AllowUnsafeUpdates = true;
- using (SPSite site = new SPSite(SPContext.Current.Site.ID))
-
- {
-
- using (SPWeb web = site.OpenWeb())
-
- {
-
- SPListItem item = GetItem("TestListSite1", 1);
-
- item.Web.AllowUnsafeUpdates = true;
-
- item["Title"] = "Update List Item";
-
- item.SystemUpdate();
-
- item.Web.AllowUnsafeUpdates = false;
-
- }
-
- }
By using above code I was successfully able to update the list item using item.SystemUpdate();