上传带有元数据的单个 Sharepoint 文档
Uploading a single Sharepoint document with metadata
我在术语库管理工具中定义了术语,并将其添加为文档库中的 "Managed Metadata" 列。
我想上传文档并更新其 "Managed Metadata" 列。
为此,我编写了以下代码:
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);
}
}
我可以上传文件并更新其常规列,但我无法更新元数据列。
有没有办法指定项目["Metadata"] GUID?
可以在术语库中找到术语指南:
添加对 Microsoft.SharePoint.Client.Taxonomy.dll 的引用:
这是使用 TaxonomyFieldValue class 设置托管元数据字段值的代码片段:
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();
}
我在术语库管理工具中定义了术语,并将其添加为文档库中的 "Managed Metadata" 列。 我想上传文档并更新其 "Managed Metadata" 列。 为此,我编写了以下代码:
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);
}
}
我可以上传文件并更新其常规列,但我无法更新元数据列。
有没有办法指定项目["Metadata"] GUID?
可以在术语库中找到术语指南:
添加对 Microsoft.SharePoint.Client.Taxonomy.dll 的引用:
这是使用 TaxonomyFieldValue class 设置托管元数据字段值的代码片段:
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();
}