0

I know this question has been asked in the past, but I can't seem to find a working solution. I want to update The CreatedDateTime, ModifiedBy and CreatedBy metadata on a file that I uploaded with CSOM. I have this code. It does not crash, but does not update the properties either.

        FileCreationInformation newFile = new FileCreationInformation();
        byte[] FileContent = null;
       
        using (var webClient = new WebClient())
        {
            webClient.Headers.Add("Authorization", authorization);
            FileContent = webClient.DownloadData(file.DowlnloadUrl);
        }

        newFile.ContentStream = new System.IO.MemoryStream(FileContent);
        newFile.Url = System.IO.Path.GetFileName(file.Name);
        Microsoft.SharePoint.Client.File sharepointFile = _spFolder.Files.Add(newFile);
        _context.ExecuteQuery();

        sharepointFile.ListItemAllFields["Modified"] = DateTime.Today.AddDays(-3);
        sharepointFile.ListItemAllFields["Created"] = DateTime.Today.AddDays(-3);
        sharepointFile.ListItemAllFields.Update();
        _context.ExecuteQuery();

The file still appears that it was created/modified a few seconds ago.

enter image description here

Any help would be appreciated. PS: if this can be done in one go (one single ExecuteQuery), that would be great.

4
  • Have you tried to update other fields like title? Will it work? Commented Sep 8, 2021 at 8:40
  • @MichaelHan_MSFT Yes, it seems to work with Title, but it creates a new version where the Title appears. I tried adding disabling the versioning and updating the Dates, but still no luck.
    – Rafa Ayadi
    Commented Sep 8, 2021 at 11:10
  • Ok you could try to set the Modified field property ReadOnlyField to false, then updating the dates. Commented Sep 9, 2021 at 9:36
  • That did not help either.
    – Rafa Ayadi
    Commented Sep 9, 2021 at 22:26

2 Answers 2

0

Try this:

uploadFile.CheckOut();

ListItem item = uploadFile.ListItemAllFields;
item["FieldName"] = "SomeFieldValue";
item.Update();

// use OverwriteCheckIn type to make sure not to create multiple versions 
uploadFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);

See reference: update document properties in library using csom

or check

https://stackoverflow.com/questions/9847935/upload-a-document-to-a-sharepoint-list-from-client-side-object-model

1
  • Hi Martin. I have seen this thread and it is still not working in my case. It is very odd. I have tried multiple solutions on stackoverflow yet nothing worked for me. (Tried disabling and enabling Versionning, and it did not work either).
    – Rafa Ayadi
    Commented Sep 7, 2021 at 21:27
0

This was very tricky. My app is running on .NetCore 3.1. Below is the endpoint I am using to generate a Token and then a ClientContext

            var body = $"resource={resource}&client_id={_clientId}&grant_type=password&username={HttpUtility.UrlEncode(_userName)}&password={HttpUtility.UrlEncode(pwd)}";

The clientId is my Azure App Client Id. This code generates a Bearer token. When you decode the token, you can see the permissions you have with it. I needed the SitesFullControl delegated permission. enter image description here

Once I added that permission, my Bearer token decode had it and my Update ModifiedDate worked. I hope this can help anyone who spent days trying to figure this out like I did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.