Friday 13 March 2015

SharePoint : Update() vs SystemUpdate() vs UpdateVersionOverwrite()

As Arnold in 'The Terminator' I am back as a savior but ofcourse with SharePoint solutions. Today I will tell you the difference between 3 little methods to update a SharePoint list item that often confuse people new to SharePoint. It confuses because all the three functions have same basic functionality i.e. update an item. Then how to decide which one to use? Below are the differences :

                 Update                                SystemUpdate                              UpdateOverwriteVersion        

Updates the item in database


Updates the item in database

Updates the item in database

Updates the 'modified' and 'modified by' value

Doesn't change the 'modified' and 'modified by' fields. Updates the 'modified' and 'modified by' value

Creates a new Version

Doesn't create a new version Doesn't create a new version

NOTE : While working on one of such requirements (in SharePoint 2013) I have found out that UpdateOverwriteVersion doesn't increment the minor versions but if the version of an item is a major it increments its version. For example if your item version is 2.0 it may change it to 2.1. If you are not okay with that you can use SystemUpdate. If you want to preserve the metadata i.e. if you want the "modified" and "modified by" fields to remain unchanged you can add these to lines to your code :

$item["modified"] = $item["modified"]
$item["modified by"] = $item["modified by"]

(For SystemUpdate you don't have worry about it. But if these 2 fields are getting updated you can preserve it like this.)
what it does is keeps your field values unchanged. If it doesn't work you can try it with $web.AllowUnsafeUpdate=$true. After you are done with all the changes you can set it to false. 'AllowUnsafeUpdate=$false' protects you from cross site scripting.

Okay... this is all I had to say on this topic. I have decided to feed you with some interesting facts about SharePoint at the end of every article from now on.

FACT : In SharePoint a list or library item can only occupy a maximum of 8000 bytes in content database out of which 256 bytes are reserved for built-in columns.

No comments:

Post a Comment

Feel free to share your thoughts...