Friday, 15 April 2011

sharepoint 2013 CSOM to make a termset as 'navigation termset', last line of code throw exception -



sharepoint 2013 CSOM to make a termset as 'navigation termset', last line of code throw exception -

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 offline or missing"); termstore termstore = taxonomysession.termstores[0]; clientcontext.load(termstore, ts => ts.name, ts => ts.workinglanguage); clientcontext.executequery(); // termset object 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 throw exception "the object in invalid state"

most of code works well, termsets created, lastly executequery throw exception when seek create termset navigation termset.

try next code. it's working fine.

public void setmanagednavigationin(string termsetname,guid termsetguid) { seek { taxonomysession taxonomysession = taxonomysession.gettaxonomysession(this.ctx); taxonomysession.updatecache(); this.ctx.load(taxonomysession,ts => ts.termstores); this.ctx.executequery(); //throw error if no term stores 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 new term grouping site collection termgroup sitecollectiongroup = termstore.getsitecollectiongroup(this.ctx.site, createifmissing: true); termset termset = sitecollectiongroup.createtermset(termsetname, termsetguid, termstore.workinglanguage); //commit changes termstore.commitall(); this.ctx.executequery(); } //reset web object 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 cache taxonomynavigation.flushsitefromcache(this.ctx, this.ctx.site); this.ctx.executequery(); } grab (exception ex) { throw new deploymentexception("managed navigation setting issue", ex); } }

sharepoint csom

No comments:

Post a Comment