c# - How do I optimize this SharePoint query for Terms? -


i have managed metadata service has term group term set , terms.

i new sharepoint querying , doing following:

  1. get client context
  2. set credentials
  3. update client context cache
  4. get taxonomy session
  5. get term store
  6. get term set
  7. get terms

for every 1 of above steps, loading , executing query on client context.

  1. is there better way load , execute queries?
  2. is there direct linq query or caml query can use terms need given uid of term set?

code:

var siteurl = confighelper.getvalue("sharepointsiteurl"); var clientcontext = new clientcontext(siteurl);  clientcontext.credentials = new networkcredential(confighelper.getvalue("serviceaccountlogonname"), confighelper.getvalue("serviceaccountpassword"));  var 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"); }  var termstore = taxonomysession.termstores[1];  clientcontext.load(termstore); clientcontext.executequery();  var termset = termstore.gettermset(new guid("f40eeb54-7c87-409d-96c7-75ceed6bff60")); clientcontext.load(termset); clientcontext.executequery();  var terms = termset.getallterms(); clientcontext.load(terms); clientcontext.executequery();  foreach (var term in terms) {     clientcontext.load(term, t => t.id, t => t.name);     clientcontext.executequery();                 } 

how optimize sharepoint query terms?

the main issue specified example bunch of intermediate requests submitted server, main optimization be:

  • eliminate intermediate requests (see modified example below )
  • or since sharepoint csom supports request batching multiple client object requested server using single batch request only

since if goal retrieve terms only specif term set, example optimized demonstrated below:

var taxonomysession = taxonomysession.gettaxonomysession(ctx); var termstore = taxonomysession.getdefaultsitecollectiontermstore(); var termset = termstore.gettermset(termsetid); var terms = termset.getallterms(); ctx.load(terms, tcol => tcol.include(t => t.id,t => t.name)); ctx.executequery(); 

some recommendations

  • prefer termstorecollection.getbyname or termstorecollection.getbyid methods on getting termstore index since in later case termstorecollection have initialized first
  • use taxonomysession.getdefaultsitecollectiontermstore method if need default term store

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -