0

We have a requirement to use CSOM based .Net Managed Code to perform the upload file and set the metadata properties. However, we cannot upload and set the metadata properties simultaneously using CSOM code. This can be done using PNP Powershell using the following command

$upload = Add-PnPFile -Path 'SourceFilePath' -Folder 'TargetFolderPath' -NewFileName 'Destination Filename' -Values 'Metadata Properties' -ErrorAction Stop

I am trying to seek the help of experts if there is any other way of achieving this using CSOM .Net Managed Code, even any PNP Core Nuget Packages.

1 Answer 1

0

Update: Use the below code to update metadata without creating a new version.

  • CheckOut the file before updating metadata and then check-in by overwriting the check-in parameter.
        uploadedFile.CheckOut();
        ListItem item = uploadedFile.ListItemAllFields;
        //Set the metadata  
        string docTitle = string.Empty;
        item["Title"] = "Dev";
        item.Update();
        uploadedFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);
        clientContext.ExecuteQuery();

You can get all the List Item Properties of the uploaded file as below and update all the metadata.

    ListItem item = uploadFile.ListItemAllFields;  
    //Set the metadata  
    string docTitle = "Test Document";  
    item["Title"] = docTitle;  
    item.Update();  
    context.ExecuteQuery(); 

References:

  1. File Upload using CSOM
  2. Update Metadata

Below is the full code to upload the file and then update its property.

 File uploadedFile =  UploadFileSlicePerSlice(clientContext, "Documents", fileName, 3);

   if(uploadedFile != null)
     {
       ListItem item = uploadedFile.ListItemAllFields;
       //Set the metadata  
       string docTitle = string.Empty;
       item["Title"] = "Dev";
       item.Update();
       clientContext.ExecuteQuery();
     }

Note: Use UploadFileSlicePerSlice function mentioned in #1 reference to upload the file.

2
  • 1
    The problem with this approach is that there two versions created. First one gets created when the file is uploaded and second one gets created when the metadata is uploaded. However, if Add-pnpFile would create just 1 version.
    – suryakiran
    Jul 8, 2021 at 23:44
  • @suryakiran check the updated answer Jul 9, 2021 at 3:49

Your Answer

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

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