If you don't like long posts, go the the Conclusion section.
In many situations, we are requested to change some items/documents related properties, in general this can be done using the SharePoint user Interface, or using code (either server side code, Power Shell, or CSOM).
Modifying system related properties (ex. Author, Editor, Created , Modified) is not that simple, it should be handled in a different manner, and can be done only through code.
This post will describe the different options based on the list/document library configuration.
First, let us document the 4 properties that we are going to try to update:
SPListItem[SPBuiltInFieldId.Author]
SPListItem[SPBuiltInFieldId.Editor]
SPListItem[SPBuiltInFieldId.Created]
SPListItem[SPBuiltInFieldId.Modified]
Second, let us document the different options to update a SPListItem from code:
SPListItem.Update()
SPListItem.UpdateOverwriteVersion()
SPListItem.SystemUpdate()
Third, we will try to to use the different options, and check the outcome of each.
Update()
The code snippet below tries to override the 4 properties, and uses the Update() of the list item, the outcome of this test ends up as follows:
SPListItem[SPBuiltInFieldId.Author] ==> Changed as requested
SPListItem[SPBuiltInFieldId.Editor] ==> Changed as requested
SPListItem[SPBuiltInFieldId.Created] ==> Changed as requested
SPListItem[SPBuiltInFieldId.Modified] ==> Changed as requested
Notes:
1- If SPListItem[SPBuiltInFieldId.Editor] was removed, it was noticed that the SPListItem[SPBuiltInFieldId.Author] was not changed.
2- If the list/library has content approval enabled, the item/document is changed to "Pending".
3- SPListItem[SPBuiltInFieldId.Modified] must be greater than or equal to the SPListItem[SPBuiltInFieldId.Created], else it will be set to DateTime.Now by SharePoint.
4- If SPListItem[SPBuiltInFieldId.Modified] was not populated from code, it will have the default value of DateTime.Now by SharePoint.
SPListItem[SPBuiltInFieldId.Editor] ==> Changed as requested
SPListItem[SPBuiltInFieldId.Created] ==> Changed as requested
SPListItem[SPBuiltInFieldId.Modified] ==> Changed as requested
Notes:
1- If SPListItem[SPBuiltInFieldId.Editor] was removed, it was noticed that the SPListItem[SPBuiltInFieldId.Author] was not changed.
2- If the list/library has content approval enabled, the item/document is changed to "Pending".
3- SPListItem[SPBuiltInFieldId.Modified] must be greater than or equal to the SPListItem[SPBuiltInFieldId.Created], else it will be set to DateTime.Now by SharePoint.
4- If SPListItem[SPBuiltInFieldId.Modified] was not populated from code, it will have the default value of DateTime.Now by SharePoint.
public void Update(SPFieldUserValue oUser, SPListItem item) { item[SPBuiltInFieldId.Author] = oUser; item[SPBuiltInFieldId.Editor] = oUser; // use line below instead if only Author is required to be changed //item[SPBuiltInFieldId.Editor] = item[SPBuiltInFieldId.Editor]; item[SPBuiltInFieldId.Created] = DateTime.Now; item[SPBuiltInFieldId.Modified] = DateTime.Now; item.Update(); }
UpdateOverwriteVersion()
The code snippet below tries to override the 4 properties, and uses the UpdateOverwriteVersion() of the list item, the outcome of this test ends up as follows:
SPListItem[SPBuiltInFieldId.Author] ==> Changed as requested
SPListItem[SPBuiltInFieldId.Editor] ==> Changed as requested
SPListItem[SPBuiltInFieldId.Created] ==> Changed as requested
SPListItem[SPBuiltInFieldId.Modified] ==> Changed as requested
Notes:
1- If SPListItem[SPBuiltInFieldId.Editor] was removed, it was noticed that the SPListItem[SPBuiltInFieldId.Author] was not changed.
2- If the list/library has content approval enabled, the item/document is changed to "Pending".
3- SPListItem[SPBuiltInFieldId.Modified] must be greater than or equal to the SPListItem[SPBuiltInFieldId.Created], else it will be set to DateTime.Now by SharePoint.
4- If SPListItem[SPBuiltInFieldId.Modified] was not populated from code, it will have the default value of DateTime.Now by SharePoint.
SPListItem[SPBuiltInFieldId.Editor] ==> Changed as requested
SPListItem[SPBuiltInFieldId.Created] ==> Changed as requested
SPListItem[SPBuiltInFieldId.Modified] ==> Changed as requested
Notes:
1- If SPListItem[SPBuiltInFieldId.Editor] was removed, it was noticed that the SPListItem[SPBuiltInFieldId.Author] was not changed.
2- If the list/library has content approval enabled, the item/document is changed to "Pending".
3- SPListItem[SPBuiltInFieldId.Modified] must be greater than or equal to the SPListItem[SPBuiltInFieldId.Created], else it will be set to DateTime.Now by SharePoint.
4- If SPListItem[SPBuiltInFieldId.Modified] was not populated from code, it will have the default value of DateTime.Now by SharePoint.
public void UpdateOverrideVersion(SPFieldUserValue oUser, SPListItem item,DateTime dtStamp) { item[SPBuiltInFieldId.Author] = oUser; item[SPBuiltInFieldId.Editor] = oUser; // use line below instead if only Author is required to be changed //item[SPBuiltInFieldId.Editor] = item[SPBuiltInFieldId.Editor]; item[SPBuiltInFieldId.Created] = dtStamp; item[SPBuiltInFieldId.Modified] = dtStamp; item.UpdateOverwriteVersion(); }
SystemUpdate()
The code snippet below tries to override the 4 properties, and uses the SystemUpdate() of the list item, the outcome of this test ends up as follows:
SPListItem[SPBuiltInFieldId.Author] ==> Changed as requested
SPListItem[SPBuiltInFieldId.Editor] ==> No Effect
SPListItem[SPBuiltInFieldId.Created] ==> No Effect
SPListItem[SPBuiltInFieldId.Modified] ==> No Effect
Notes:
1- If SPListItem[SPBuiltInFieldId.Editor] was removed, it was noticed that the SPListItem[SPBuiltInFieldId.Author] was not changed.
2- If the list/library has content approval enabled, the item/document remain on its state. (ex. Approved)
3- Editor, Created, Modified fields were not affected by the update.
SPListItem[SPBuiltInFieldId.Editor] ==> No Effect
SPListItem[SPBuiltInFieldId.Created] ==> No Effect
SPListItem[SPBuiltInFieldId.Modified] ==> No Effect
Notes:
1- If SPListItem[SPBuiltInFieldId.Editor] was removed, it was noticed that the SPListItem[SPBuiltInFieldId.Author] was not changed.
2- If the list/library has content approval enabled, the item/document remain on its state. (ex. Approved)
3- Editor, Created, Modified fields were not affected by the update.
public void SystemUpdate(SPFieldUserValue oUser, SPListItem item) { item[SPBuiltInFieldId.Author] = oUser; item[SPBuiltInFieldId.Editor] = oUser; //No Effect, if removed, Author will not be updated item[SPBuiltInFieldId.Created] = DateTime.Now; //No Effect item[SPBuiltInFieldId.Modified] = DateTime.Now; //No Effect //system update will only allow changing the Author, even if other fields were set to other values item.SystemUpdate(false); }
Revised SystemUpdate() method should look like:
public void RevisedSystemUpdate(SPFieldUserValue oUser, SPListItem item) { item[SPBuiltInFieldId.Author] = oUser; item[SPBuiltInFieldId.Editor] = item[SPBuiltInFieldId.Editor]; //No Effect, if removed, Author will not be updated //system update will only allow changing the Author, even if other fields were set to other values item.SystemUpdate(false); }
Conclusion
1- Use SystemUpdate() if you want to override the Author only, and maintain the item Approval state.
2- Set Editor to current value even if you don't want to override it, this will allow SharePoint to update the Author property.
3- Update() and UpdateOverwriteVersion() will change approval state to Pending, you might need to check if the item was already approved, and re-approve it as needed.
No comments:
Post a Comment