Search code examples
sharepointsharepoint-2013csom

SharePoint 2013 CSOM Upload Doc and Change Column Values


I'd like to upload a document to a SharePoint 2013 document library and set value for three of the columns.

I'm running following C# code from a unit test within Visual Studio:

using (var ctx = new ClientContext($"{spRoot}/{spPathToFolder}"))
            {
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, targetFileUrl, ms, true);

                var uploadedFile = ctx.Web.GetFileByServerRelativeUrl(targetFileUrl);
                var listItem = uploadedFile.ListItemAllFields;
                listItem["Title"] = "title";
                listItem["UPRN"] = "uprn";
                listItem["KeystoneDocType"] = "keystoneDocType";
                listItem.File.CheckIn("Added by BizTalk", CheckinType.MajorCheckIn);
                listItem.Update();
                ctx.ExecuteQuery();
            }

The following path variables values are logged:

spRoot=[https://collaboration.xxx.com], spPathToFolder=[sites/HousingICTSolution/Technical]

targetFileUrl=[/sites/HousingICTSolution/Technical/AssetMgmtEfilesDemo/xxxLogo_190213115512.png]

The file gets uploaded ok (and I can view it when I click the link withing the SharePoint library) but no column values are set. Another problem is that executing the line "ctx.ExecuteQuery()" causes the following exception to be thrown:

        Message "The file AssetMgmtEfilesDemo/xxxLogo_190213115512.png has been modified by i:0#.w|xxx\\adm-tco05544 on 13 Feb 2019 11:59:35 -0000."    string

I am user "adm-tco05544". Can anyone suggest how to prevent the exception?


Solution

  • Before updating the file item fields, please firstly checkout and then update list fields value, finally check in the file, here is a working snippet for your reference:

            ClientContext ctx = new ClientContext("http://sp/sites/dev");
            using (FileStream fs=new FileStream(@"C:\\Test.jpg",FileMode.Open))
            {
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, "/sites/dev/MyDocLibraryName/Test.jpg", fs, true);
                var uploadedFile = ctx.Web.GetFileByServerRelativeUrl("/sites/dev/MyDocLibraryName/Test.jpg");
                ctx.Load(uploadedFile);
                    ctx.ExecuteQuery();
                    if (uploadedFile.CheckOutType==CheckOutType.None)
                    {
                        uploadedFile.CheckOut();
    
                    }
    
                        var listItem = uploadedFile.ListItemAllFields;
                        listItem["Title"] = "title";
                        listItem["UPRN"] = "uprn";
                        listItem["KeystoneDocType"] = "keystoneDocType";
    
                        listItem.Update();
                        ctx.ExecuteQuery();
                        listItem.File.CheckIn("Added by BizTalk", CheckinType.MajorCheckIn);
                        ctx.ExecuteQuery();
    
    
    
    
            }
    

    enter image description here