C# 上载包含元数据的单个Sharepoint文档

C# 上载包含元数据的单个Sharepoint文档,c#,sharepoint,csom,C#,Sharepoint,Csom,我在术语库管理工具中定义了术语,并将其添加为文档库中的“托管元数据”列。 我想上传一个文档并更新其“托管元数据”列。 为此,我编写了以下代码: void UploadDocument(Document document) { try { using (ClientContext context = SPHelper.GetClientContext()) { List

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

    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?

术语GUID可在术语库中找到:

添加对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();

            }
使用(ClientContext=newclientcontext(sharePointSite))
{
FileCreationInformation FCInfo=新的FileCreationInformation();
FCInfo.Url=”http://sp2016/sites/dev/Shared%20Documents/Test.txt";
FCInfo.Overwrite=true;
FCInfo.Content=System.IO.File.ReadAllBytes(fileToUpload);
Web=context.Web;
List library=web.Lists.GetByTitle(libraryName);
Microsoft.SharePoint.Client.File uploadfile=library.RootFolder.Files.Add(FCInfo);
ListItem=uploadfile.ListItemAllFields;
项目[“标题”]=“一些数据”;
var字段=library.fields;
var field=fields.GetByInternalNameOrTitle(“managedcolumn”);
加载(字段);
加载(字段);
context.ExecuteQuery();
var taxKeywordField=context.CastTo(字段);
TaxonomyFieldValue termValue=新的TaxonomyFieldValue();
termValue.Label=“TermC”;
termValue.TermGuid=“045830f1-f51e-4bac-b631-5815a7b6125f”;
termValue.WssId=3;
taxKeywordField.SetFieldValueByValue(项,术语值);
item.Update();
context.ExecuteQuery();
uploadfile.CheckIn(“testcomment”,CheckinType.MajorCheckIn);
context.ExecuteQuery();
}

这是一个分类字段,因此您必须设置“分类字段值”。我在发帖前试过了。我的问题是它无法编译。错误1:“TaxonomyField”不包含“SetFieldValueByValue”的定义,并且找不到接受“TaxonomyField”类型的第一个参数的可访问扩展方法“SetFieldValueByValue”。错误2:类型“Microsoft.SharePoint.Taxonomy.TaxonomyField”不能用作泛型类型或方法“ClientRuntimeContext.CastTo(ClientObject)”中的类型参数“T”。没有从“Microsoft.SharePoint.Taxonomy.TaxonomyField”到“Microsoft.SharePoint.Client.ClientObject”的隐式引用转换。我使用的是Microsoft.SharePoint.Taxonomy 15.0.0I,也在SharePoint 2013中测试过,仍然有效,请确保在解决方案中引用了正确的dll。它是Microsoft.SharePoint.Client.Taxonomy.dll而不是Microsoft.Taxonomy.dllI将添加一个关于引用dll的捕获,请检查它。结果它不起作用,因为我使用的SharePoint依赖关系确实太旧了。我切换到了Microsoft.SharePoint2019.CSOM,它现在运行良好。非常感谢。