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.
Any help would be appreciated. PS: if this can be done in one go (one single ExecuteQuery), that would be great.