Question

I am copying over items from one list to another list.

After copying the "Author" field gets populated with "System Account" and so I wish to update some fields in the destination list as follows.

I have a list with a custom Person column named "OriginalAuthor".

I want to replace the value of built-in "Author" with the value of "OriginalAuthor".

I also wish to achieve the same with the modified field (which currently DOES work)

I found a few examples on-line but could not get any to work. So far I have the following:

foreach (SPListItem destItem in destList.Items)
{
     SPFieldUserValue val = new SPFieldUserValue(web.Site.RootWeb, destItem["OriginalAuthor"].ToString());

     destItem["Author"] = val;
     destItem["Modified"] = destItem["OriginalModified"];
     destItem.Update();
}
destList.Update();

The modified date gets replaced however, the Author field remains the same.

How can I get this to work?

thanks, KS

Was it helpful?

Solution 3

Well after being unable to accomplish this I have decided to use Sharepoint Content Manager to copy the items which preserves the required fields. Thanks to all for your assistance.

OTHER TIPS

Is there a reason you can't grab the Author field from the original item?

My other thought has to do with using SPListItem.SystemUpdate() instead of Update(), but that has more to do with not changing Modified/Modified By than Author.

I'm not sure and I cannot test it right now, but i think author field is a person field, so you cannot just put a name there, i think it should be an spuser or maybe some other type which references to user in SP.

Or you can also try this code:

SPList list = web.Lists["myList"];
list.Fields["Author"].ReadOnlyField = false;
SPListItem item = list.Items.Add();
item["Author"] = "value";
item.SystemUpdate(false);
list.Fields["Author"].ReadOnlyField = true;

Happy coding

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top