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:
- get client context
- set credentials
- update client context cache
- get taxonomy session
- get term store
- get term set
- get terms
for every 1 of above steps, loading , executing query on client context.
- is there better way load , execute queries?
- 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
ortermstorecollection.getbyid
methods on getting termstore index since in later casetermstorecollection
have initialized first - use
taxonomysession.getdefaultsitecollectiontermstore method
if need default term store
Comments
Post a Comment