0
votes

I'm currently building a tool to migrate from a document management system to use SharePoint Online. The main challenge I'm facing is to preserve the details of document authors and creating time. I have checked bunch of of code online but I didn't get success with any of them. Here are the approaches I used

  1. SharePoint Rest API
  2. Microsoft Graph API
  3. CSOM (using console application)

Here is the code I have so far in CSOM but I'm still not able to update the Author field

li["Title"] = "Update from CSOM";
li["Created"] = DateTime.Now.AddYears(-5);
li["Author"] = author.Id;
li.UpdateOverwriteVersion();
clientContext.ExecuteQuery();

Any idea for how to do this, or if there is any other approach to achieve my goal?

2
Hi, if the posted answer(s) resolves your question, please mark it as the answer by clicking the check mark. Doing so helps others find answers to their questions.Brian T. Jackett MSFT

2 Answers

2
votes

The code works when I did test in my environment.

using (ClientContext context = new ClientContext("https://xxx.sharepoint.com/sites/lee"))
            {
                string s = "password";
                SecureString passWord = new SecureString();
                foreach (var c in s)
                    passWord.AppendChar(c);
                context.Credentials = new SharePointOnlineCredentials("admin@xxx.onmicrosoft.com", passWord);

                var author = context.Web.EnsureUser("Lee@xxx.onmicrosoft.com");
                context.Load(author);
                context.ExecuteQuery();
                var _List = context.Web.Lists.GetByTitle("List1");
                var li = _List.GetItemById(1);

                li["Title"] = "Update from CSOM";
                li["Created"] = DateTime.Now.AddYears(-5);
                li["Author"] = author.Id;
                li.UpdateOverwriteVersion();
                context.ExecuteQuery();

            }
0
votes

You will need to update the Author and Editor fields at the same time in order to update the CreatedBy field. If you wish to update additional fields at the same time you can. Using SystemUpdate() does not update the Modified date whereas Update() does update the Modified date. See abbreviated sample below.

FieldUserValue userValue = new FieldUserValue();
User newUser = cc.Web.EnsureUser("newAuthor@xxx.onmicrosoft.com");
cc.Load(newUser);
cc.ExecuteQuery();
userValue.LookupId = newUser.Id;

item["Author"] = userValue;
item["Editor"] = userValue;

item.SystemUpdate();
cc.ExecuteQuery();