2
Guid termSetGUID = new Guid("7ab9e8b0-e1e1-4a7c-9b20-d6c5030103df");

string siteUrl = "http://win-f33ohjutmmi/sites/cms";
ClientContext clientContext = new ClientContext(siteUrl);

TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
taxonomySession.UpdateCache();

clientContext.Load(taxonomySession, ts => ts.TermStores);
clientContext.ExecuteQuery();

if (taxonomySession.TermStores.Count == 0)
     throw new InvalidOperationException("The Taxonomy Service is offline or missing");

TermStore termStore = taxonomySession.TermStores[0];
clientContext.Load(termStore,
        ts => ts.Name,
        ts => ts.WorkingLanguage);
clientContext.ExecuteQuery();

// Does the TermSet object already exist?
TermSet existingTermSet;
TermGroup siteCollectionGroup;

siteCollectionGroup = termStore.GetSiteCollectionGroup(clientContext.Site,createIfMissing: true);

existingTermSet = termStore.GetTermSet(termSetGUID);
clientContext.Load(existingTermSet);
clientContext.ExecuteQuery();
if (!existingTermSet.ServerObjectIsNull.Value)
{
     existingTermSet.DeleteObject();
     termStore.CommitAll();
     clientContext.ExecuteQuery();
}

TermSet termSet = siteCollectionGroup.CreateTermSet("CMSNavigationTermSet", termSetGUID,termStore.WorkingLanguage);

termStore.CommitAll();
clientContext.ExecuteQuery();

NavigationTermSet navTermSet = NavigationTermSet.GetAsResolvedByWeb(clientContext,termSet, clientContext.Web, "GlobalNavigationTaxonomyProvider");

navTermSet.IsNavigationTermSet = true;

termStore.CommitAll();
clientContext.ExecuteQuery();//THIS line always throw exception "The object is in invalid state"

大多数代码运行良好,创建了 TermSet,但是当我尝试将此 TermSet 作为导航 TermSet 时,最后一个 ExecuteQuery 抛出异常。

4

1 回答 1

1

试试下面的代码。它工作正常。

public void SetManagedNavigationin(string termSetName,Guid termSetGuid)
        {
            try
            {
                TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(this.Ctx);
                taxonomySession.UpdateCache();
                this.Ctx.Load(taxonomySession,ts => ts.TermStores);
                this.Ctx.ExecuteQuery();

                //Throw error if no Term Stores are found
                if (taxonomySession.TermStores.Count == 0)
                    throw new DeploymentException("TermStore Issue", new InvalidOperationException("Managed Metadata Service not found"));

                TermStore termStore = taxonomySession.TermStores[0];
                this.Ctx.Load(termStore, ts => ts.Name, ts => ts.WorkingLanguage);
                this.Ctx.ExecuteQuery();

                var existingTermSet = termStore.GetTermSet(termSetGuid);
                this.Ctx.Load(existingTermSet);
                this.Ctx.ExecuteQuery();

                if (existingTermSet.ServerObjectIsNull.GetValueOrDefault())
                {
                    // Create a new Term Group for the Site Collection
                    TermGroup siteCollectionGroup = termStore.GetSiteCollectionGroup(this.Ctx.Site,
                        createIfMissing: true);
                    TermSet termSet = siteCollectionGroup.CreateTermSet(termSetName, termSetGuid,
                        termStore.WorkingLanguage);
                    //Commit All Changes
                    termStore.CommitAll();
                    this.Ctx.ExecuteQuery();
                }

                //Reset the web object to the default settings
                var webNavigationSettings = new WebNavigationSettings(this.Ctx, this.Ctx.Web);
                webNavigationSettings.ResetToDefaults();
                webNavigationSettings.GlobalNavigation.Source = StandardNavigationSource.TaxonomyProvider;
                webNavigationSettings.GlobalNavigation.TermStoreId = termStore.Id;
                webNavigationSettings.GlobalNavigation.TermSetId = termSetGuid;
                webNavigationSettings.Update(taxonomySession);

                //flush the cache
                TaxonomyNavigation.FlushSiteFromCache(this.Ctx, this.Ctx.Site);
                this.Ctx.ExecuteQuery();
            }
            catch (Exception ex)
            {
                throw new DeploymentException("Managed Navigation setting Issue", ex);
            }
        }
于 2015-07-23T09:01:20.697 回答