There are two ways by which list item can be updated. One of them is SPListitem.Update and other is SpListitem.SystemUpdate medhod.
The below snippet is usually used to update SPListItems.
SpListItem.Update() :
The Item.Update() updates the database with the changes, creates a new version and changes the Modified on and Modified By fields.
SPList list = web.Lists["Employee"];
SPListItem item = list.Items[0]; // First item in the list
item["Designation"] = “HR”;
SPListItem item = list.Items[0]; // First item in the list
item["Designation"] = “HR”;
item.Update();
list.Update();
list.Update();
The last line in the code above "item.Update()" does the final task of updating all the assigned value for that specific item within the SPList.
But is this the only way to update an item? No, we can also update the same item using
But is this the only way to update an item? No, we can also update the same item using
item.SystemUpdate();
Then what is the difference between the two?
With item.Update(), we update the changes that are made to the list item. Is that all what it does? No, internally it also updates the "ModifiedBy" and "ModifiedOn" fields as per the current logged in user and current server time. Optionally it also updates the version of the item if the Versioning option is turned on for that specific list.
SpListItem.SystemUpdate() :
If you want to avoid version changes, Modified and Modified By fields from getting updated , Item.SystemUpdate() would be the right way to do it.
SPList list = web.Lists["Employee"];
SPListItem item = list.Items[0];
item["Designation"] = “HR”;
SPList list = web.Lists["Employee"];
SPListItem item = list.Items[0];
item["Designation"] = “HR”;
item.SystemUpdate();
list.Update();
list.Update();
So, at any point if we wish not to update these extra things i.e. the "ModifiedOn", "ModifiedBy" and the "Item Version", then the solution for it is to use item.SystemUpdate() instead of item.Update(). This will help you in updating only those fields which are specified within your code blocks.
No comments:
Post a Comment