I am building a migration tool that is migrating items from an XML
schema to SharePoint list fields. Everything is working fine except when it comes to the migration of Created By
fields. I need to use the client object model to do this.
So far this is what I have:
using (ClientContext ctx = new ClientContext(url))
{
List lst = ctx.Web.Lists.GetByTitle("list name");
Field field = lst.Fields.GetByTitle("Created By");
field.ReadOnlyField = false; //Allow the field to be modified
//...Get all list items and store in ListItemCollection named 'col'
foreach (ListItem item in col)
{
item["Author"] = xmlNodeValue; //Take the User form the xml schema and insert into sharepoint's created by field
item.Update();
ctx.Credentials = creds;
ctx.ExecuteQuery();
}
}
The exception thrown when executing against the server:
Invalid data has been used to update the list item. The field you are trying to update may be read only.
I realize that I probably can't store a string of xmlNodeValue
in that field, so how can I achieve this?
Also please keep in mind that the user name pulled from xmlNodeValue
may not exist as a claims user.
sourceCtx.Web.EnsureUser(val.LookupValue)
is bad, because use user display text for find object user. UsesourceCtx.Web.GetUserById(val.LookupId)