SharePoint 2013 CSOM上载文档和更改列值
我想将文档上载到SharePoint 2013文档库,并为其中三列设置值 我正在Visual Studio中的单元测试中运行以下C代码:SharePoint 2013 CSOM上载文档和更改列值,sharepoint,sharepoint-2013,csom,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);
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。有人能建议如何防止异常吗?在更新文件项字段之前,请先签出,然后更新列表字段值,最后签入文件,以下是一个工作片段供您参考:
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();
}
在执行签出时,引发以下异常:{文件\\由i:0.w | leeds\\adm-tco05544签出以进行编辑。}这意味着该文件已签出,然后删除uploadedFile.checkout函数,并请确保在签入文件之前listItem.Update。我将更新上面的代码片段,请检查。谢谢@Jerry_MSFT,这在我删除CheckOut命令后起了作用。