2014-06-19 57 views
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" 

大部分代碼的效果很好,TermSets獲得創建代碼拋出異常的最後一行,但最後的executeQuery拋出異常時,我儘量讓這個TermSet作爲導航TermSet。的SharePoint 2013 CSOM做出termset爲「導航termset」,

+0

您的代碼適用於我(在SharePoint 2013上使用最新更新嘗試過)。 也嘗試clientContext.Load(clientContext.Web);在GetAsResolvedByWeb之前 –

回答

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); 
      } 
     } 
相關問題