gpt4 book ai didi

c# - 上传带有元数据的单个 Sharepoint 文档

转载 作者:行者123 更新时间:2023-11-30 17:25:16 26 4
gpt4 key购买 nike

我在术语库管理工具中定义术语,并将其添加为文档库中的“托管元数据”列。我想上传文档并更新其“托管元数据”列。为此,我编写了以下代码:

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

我可以上传文件并更新其常规列,但我无法更新元数据列。

有没有办法指定项目["元数据"] GUID?

最佳答案

可以在术语库中找到术语指南:

enter image description here

添加对 Microsoft.SharePoint.Client.Taxonomy.dll 的引用:

enter image description here

这是使用 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();

}

enter image description here

关于c# - 上传带有元数据的单个 Sharepoint 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58875767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com