我在术语库管理工具中定义了术语,并将其作为文档库中的“托管元数据”列添加。 我想上传文档并更新其“托管元数据”列。 为此,我编写了以下代码:
void UploadDocument(Document document)
{
try
{
using (ClientContext context = SPHelper.GetClientContext())
{
List library = context.Web.Lists.GetByTitle("MyDocumentLibrary");
FileCreationInformation fileInfo = new FileCreationInformation
{
Url = "MyFileTarget",
Content = document.Content,
Overwrite = true
};
File file = library.RootFolder.Files.Add(fileInfo);
ListItem item = file.ListItemAllFields;
item["RegularColumn"] = "some data";
item["Metadata"] = "some other data";
item.Update();
context.ExecuteQuery(); // "The given guid does not exist in the term store." Exception thrown
}
}
catch (Exception ex)
{
LogHelper.RecordError("Failed to upload document", ex, System.Reflection.MethodInfo.GetCurrentMethod().Name);
}
}
我可以上传文件并更新其常规列,但不能更新元数据列。
是否可以指定item [“元数据”] GUID?
答案 0 :(得分:1)
可以在术语库中找到术语指南:
添加对Microsoft.SharePoint.Client.Taxonomy.dll的引用:
以下是使用TaxonomyFieldValue类设置托管元数据字段值的代码段:
using (ClientContext context = new ClientContext(sharePointSite))
{
FileCreationInformation FCInfo = new FileCreationInformation();
FCInfo.Url = "http://sp2016/sites/dev/Shared%20Documents/Test.txt";
FCInfo.Overwrite = true;
FCInfo.Content = System.IO.File.ReadAllBytes(fileToUpload);
Web web = context.Web;
List library = web.Lists.GetByTitle(libraryName);
Microsoft.SharePoint.Client.File uploadfile = library.RootFolder.Files.Add(FCInfo);
ListItem item = uploadfile.ListItemAllFields;
item["Title"] = "some data";
var fields = library.Fields;
var field = fields.GetByInternalNameOrTitle("managedcolumn");
context.Load(fields);
context.Load(field);
context.ExecuteQuery();
var taxKeywordField = context.CastTo<TaxonomyField>(field);
TaxonomyFieldValue termValue = new TaxonomyFieldValue();
termValue.Label = "TermC";
termValue.TermGuid = "045830f1-f51e-4bac-b631-5815a7b6125f";
termValue.WssId = 3;
taxKeywordField.SetFieldValueByValue(item, termValue);
item.Update();
context.ExecuteQuery();
uploadfile.CheckIn("testcomment", CheckinType.MajorCheckIn);
context.ExecuteQuery();
}