torsdag 5. desember 2013

Override values of Created By and Modified By fields

In some cases, it could be useful (or maybe even necessary) to be able to override the values in a SPListItem's Created By (Author as Internal Name) and Modified By (Editor as Internal Name).

So, how can you accomplish this? Simple!


web.AllowUnsafeUpdates = true;

item["Editor"] = string.Format("{0};#{1}", spUser.ID, spUser.Name);
item["Author"] = string.Format("{0};#{1}", spUser.ID, spUser.Name);

item.UpdateOverwriteVersion();

web.AllowUnsafeUpdates = false;


The key is item.UpdateOverwriteVersion(). item.Update() and item.SystemUpdate() won't work here. Created By and Modified By are read-only fields and aren't meant to be messed with. However, I have been in situations where I had to modify them using code.

----
Horns up \m/