I have the following issue: I want to upload a file to a library (with CSOM C#) that automatically declares all new files/items as records. The site collection setting for records is set to prevent editing and deletion of records.

The normal way to do this with CSOM is to first add the file, then get the "ListItemAllFields" of the uploaded file, set the field values on this item and then use "ExecuteQuery" to persist everything:

FileCreationInformation fci = new FileCreationInformation
    Overwrite = true,
    Url = targetUrl,
    Content = System.IO.File.ReadAllBytes(sipPackage.FullName),
Microsoft.SharePoint.Client.File uploadedFile = targetFolder.Files.Add(fci);

ListItem item = uploadedFile.ListItemAllFields;
item["Created"] = creationDate;
item["Modified"] = creationDate;
item["FieldXy"] = "Test";

// Persist changes
ctx.Load(uploadedFile, f => f.CheckOutType, f => f.ListItemAllFields["EncodedAbsUrl"]);

But this fails in my scenario, because the file directly is being declared as a record after upload. When I then set the field values the following exception is thrown (on last line of above code):

{Microsoft.SharePoint.Client.ServerException: This item cannot be updated because it is locked as read-only. at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream) at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse() at ...

I don't want to change the site's record setting to allow editing.

Is there a way to upload a file directly with metadata? In CSOM there seems to be no overload for this.

(un)funny thing is. When I upload a file to the same library manually using a browser and it has not all required fields set, it will not be a record. I then can edit the file, add the missing field value and after saving it, it will be a record.

How could I solve this issue?

  • In the meantime I could switch to SharePoint 2016 as this would be the target version anyway. SP2016 has methods to declare items as records and also to undeclare them. We can do this even if we cannot undeclare them manually through the browser. So I now simply undeclare the item before the update. The update will automatically make the item a record again. But would be great to know if there would be another solution for SP2013. – Shihan May 9 '19 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.