使用元数据上传单个Sharepoint文档

问题描述 投票:3回答:1

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

    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?

c# sharepoint csom
1个回答
1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.