Search code examples
microsoft-graph-apisharepoint-onlinesharepoint-rest-api

Can't preserve document core metadata (Created By, Modified By) when I import documents to SharePoint Document Library


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?


Solution

  • 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("[email protected]", passWord);
    
                    var author = context.Web.EnsureUser("[email protected]");
                    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();
    
                }