SharePoint 2013 CSOM上载文档和更改列值

时间:2019-02-13 12:05:04

标签: sharepoint sharepoint-2013 csom

我想将文档上传到SharePoint 2013文档库,并为其中三列设置值。

我正在从Visual Studio的单元测试中运行以下C#代码:

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();
            }

记录以下路径变量值:

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

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

文件可以正常上传(当我单击SharePoint库中的链接时可以查看它),但未设置任何列值。另一个问题是,执行“ ctx.ExecuteQuery()”行会引发以下异常:

        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

我是用户“ adm-tco05544”。谁能建议如何防止该异常?

1 个答案:

答案 0 :(得分:1)

在更新文件项字段之前,请先签出,然后更新列表字段值,最后签入文件,这是一个有效的代码段供您参考:

        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