Setting Author Field In SPListItem Won't Persist
I am trying to copy an SPListItem (with file) from one site collection to another. I do this by creating the file like this:
var archiveFile = newsArchive.Lists[listName].RootFolder.Files.Add(originalItem.File.Name, originalItem.File.OpenBinary()); var archiveItem = archiveFile.Item;
through a utility method I wrote i then set all field values of the new item to correspond with the original item like so
The thing is, this does not persist the Author field.
I tried setting the Author field explicitely in every way imaginable, for instance like so:
string userName = originalItem.GetUser("Created by").LoginName; SPUser user = newsArchive.SiteUsers[userName]; archiveItem["Author"] = user.ID + ";#" + user.LoginName; archiveItem.Update();
And like so
string userName = originalItem.GetUser("Created by").LoginName; SPUser user = newsArchive.SiteUsers[userName]; archiveItem["Author"] = user; archiveItem.Update();
But as soon as the SPListItem.Update() method is called, the archiveItem["Author"] field has reverted to sharepoint\system. I'm a bit at a loss here, this should work..
P.S. the SPListItem.GetUser method is an extension method
P.P.S. Code is being run from a timer job...
Edit: Did some more digging by adding a new field to the content type and then setting that field to reflect the Author of the original item, but that is not set either. However, the web.EnsureUser(username) does return the correct user. Is this weird or what!?!
Found the answer, using
SPFieldUserValue val = new SPFieldUserValue(newsArchive, user.ID, user.Name); archiveItem["Author"] = val; archiveItem.SystemUpdate(false);
did the trick!
- → Licensing system for client side code web application
- → Can ASP.NET AJAX partial rendering work inside a SharePoint 2007 application page?
- → Windows Sharepoint Services (WSS) and Forms Authentication - Passing those credentials to other ASP.NET Forms Authentication Apps
- → Bypass invalid SSL certificate errors when calling web services in .Net
- → Errors creating WebPart subclass in another assembly
- → Do I have to use InfoPath forms for every task within a SharePoint workflow?
- → How do I check if a Sharepoint Document Library has the Require Approval flag set, using the Sharepoint API?
- → Jquery - JS Clear table before new search, whitin function or out?
- → Automatically open Office add-in on SharePoint Online
- → Knockout unable to process binding in a specific function
- → Reformat list to grouped structure