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」,
您的代碼適用於我(在SharePoint 2013上使用最新更新嘗試過)。 也嘗試clientContext.Load(clientContext.Web);在GetAsResolvedByWeb之前 –