0

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?

Joseph Wahba
  • 439
  • 2
  • 5
  • 16
  • 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 May 26 '20 at 01:51

2 Answers2

2

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();

            }
Lee
  • 5,305
  • 1
  • 6
  • 12
  • 1
    It turns out that this code works fine for any document library except the details "Documents" library created in the site, pretty strange but I tested it with other sites with the same results and I got the same results – Joseph Wahba Jun 22 '20 at 09:00
0

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();
  • See a similar example here that was targeted at SP2016: [https://sharepoint.stackexchange.com/questions/256991/how-to-update-author-created-by-field-in-sharepoint-2016-using-csom-programati](https://sharepoint.stackexchange.com/questions/256991/how-to-update-author-created-by-field-in-sharepoint-2016-using-csom-programati) – Brian T. Jackett MSFT May 11 '20 at 19:12