Saturday, September 27, 2014

Preserve the “Last modified” and “Modified by” field values for the SharePoint list

What is the general approach we follow when we try to add / edit a SPListItem using the SharePoint object model? Yes I know it! Almost everybody will have a common answer to this (which is something similar to what is provided below).

SPListItem item = SPList.Items.Add();
item["Column1"] = "value for column 1";
item["Column2"] = "value for column 2";
item.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
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.
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