尝试从contenttypehub获取内容类型并将其添加到网站集中时出错

时间:2018-07-11 10:21:36

标签: sharepoint sharepoint-online

所以我想做的是创建一个新的网站集,然后在其上添加一些我想从内容类型中心获取的内容类型。

到目前为止,我已经知道了:

using (Context)
        {
            Microsoft.SharePoint.Client.ContentTypeCollection ctColl = null;
            List<Microsoft.SharePoint.Client.ContentType> toAddCts = new List<Microsoft.SharePoint.Client.ContentType>();

            using (ClientContext cthubcontext = Context.Clone(".../sites/contenttypehub/"))
            {
                ctColl = cthubcontext.Web.ContentTypes;
                cthubcontext.Load(ctColl);
                cthubcontext.ExecuteQueryRetry();

                foreach (var ct in ctColl)
                {
                    if (site.ContentTypes.IndexOf(ct.Name) > -1)
                    {
                        toAddCts.Add(ct);
                    }
                }

                Microsoft.SharePoint.Client.ContentTypeCollection siteContentTypes = Context.Web.ContentTypes;
                Context.Load(siteContentTypes);
                Context.ExecuteQueryRetry();

                foreach (var ct2 in toAddCts)
                {
                    siteContentTypes.AddExistingContentType(ct2);
                }
            }
        }  

“上下文”是登录网站本身时的上下文。我尝试从contenttypehub获取新的上下文并获取所有内容类型。然后,我过滤并将所需的内容类型添加到自己的列表中。然后,我尝试获取网站上下文的所有内容类型。然后,我迭代foreach我要添加的自己的内容类型列表,并尝试将其添加到我的siteContentTypes中。

但是这会导致以下错误:

the object is used in the context different from the one associated with the object.  

任何人都知道如何进行这项工作?还是有一种更简单的方法来向最近创建的网站集添加(现有)内容类型?还是在创建网站集时可以传递内容类型?

0 个答案:

没有答案