Update "Created By" or "Modified By" filed of a File in Sharepoint using CSOM

Viewed 33

I got stuck in a situation. I uploaded a file in Sharepoint programmatically in C# using the CSOM library,

FileCreationInformation newfile = new FileCreationInformation();
        byte[] FileContent = dstStream.ToArray();
        newfile.ContentStream = new MemoryStream(FileContent);
        newfile.Url = strfilename;
        
        Microsoft.SharePoint.Client.File uploadfile = destiFolder.Files.Add(newfile);
        context.Load(uploadfile);
        
        context.ExecuteQuery();

I used above mentioned approach to upload a file in a particular folder.

Now the thing is I need to update that file's 'Created By'/'Modified By' field with another Username.

So can anybody help me out of this ?

1 Answers

You can update list item field values

// checkout
uploadfile.CheckOut();

// get list item field values
ListItem item = uploadfile.ListItemAllFields;

// ensure User object, specify the correct login name
User anotherUser = context.Web.EnsureUser("Contoso\\JohnDoe");
context.Load(anotherUser);
context.ExecuteQuery();

// update Author and Editor field
item["Editor"] = anotherUser;
item["Author"] = anotherUser;

// update item
item.Update();

// checkin
uploadfile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);

context.ExecuteQuery();
Related