Ad

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

Utilities.PopulateListItemMetadata(....)

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!?!

Ad

Answer

Found the answer, using

SPFieldUserValue val = new SPFieldUserValue(newsArchive, user.ID, user.Name);
archiveItem["Author"] = val; 
archiveItem.SystemUpdate(false);

did the trick!

Ad
source: stackoverflow.com
Ad