0

I'm currently developing a WebPart for a client using SPFX, React JS and PNP JS where I need to get a full list of Countries from a Countries taxonomy at Tenant level to allow my to populate a dropdown to filter items against, My code is currently returning 18 of the 205 countries and I can't figure out why, for some reason I am getting 2 termSets back to get terms from, both of which return the same 18 terms. Only 1 of the Termstores is actually visible to me.

Here is my code for item retrieval

 export function updateSelections(Set, updateState){
  taxonomy.termStores.getByName(definitions.TermStore).get().then( baseStore => {
    TermStore = baseStore;
    baseStore.getTermSetsByName(Set, definitions.languageCode).get().then( thisTermSets => {
      TermSets[Set] = {};
      thisTermSets.forEach(thisTermSet => {
        updateState(thisTermSet);
        thisTermSet.terms.get().then(setTerms => {
          setTerms.forEach(setTerm => {
            const termGUID = setTerm.Id.replace('/Guid(','').replace(')/','');
            setTerm.Id = termGUID;
            TermSets[Set][termGUID] = setTerm;
          });
        });
      });
    });
  }); 
}

The values being passed in are the Termset name and a function to store terms in state. TermSets is a global variable that stores values for later internal calls to reduce the number of SharePoint API calls made. The function is generic as I'm using it to retrive various different termsets and only the Countries termset seems to be causing me this issue.

Here is an overview of the code without enclosed functions etc.

  taxonomy.termStores.getByName(TAXONOMYREFERENCEID).get().then( baseStore => {
baseStore.getTermSetsByName('Country', definitions.languageCode).get().then( TermSets => {
  TermSets.forEach(TermSet => {
    TermSet.terms.get().then(Terms => {
      Store terms in memory
      });
    });
  });
});

});

Can anyone shed light on where I'm going wrong.

2
  • 1
    Here's a suggestion: don't post your actual solution code, write a very specific sample that illustrates as exactly and succinctly as possible the issue you are facing. Hard code the variables and eliminate the abstractions. Very often when you do this you gain some insight into what's going on, and even if not, answerers will have a much easier time understanding your code. Ideally I could take your example and run it, but with so many unknowns here there's no chance of that. Oct 16, 2019 at 15:36
  • 1
    thanks Derek, I will do this just now Oct 16, 2019 at 16:00

1 Answer 1

0

I use this to retrieve all terms of a termset:

  const tbatch = taxonomy.createBatch();
      return taxonomy.termStores.getByName("Taxonomy_InjkMqAvKdUBok59vNz/Mg==").get().then((resp1: ITermStore) => {
        return resp1.getTermGroupById("7bdebaed-a684-4682-a5a6-6c1f1e976641").termSets.get().then((resp2: ITermSet[]) => {
          resp2.forEach((ele: ITermSet) => {
            ele.terms.select('Name', 'Id').inBatch(tbatch).get().then((resp3: ITerm[]) => {
              resp3.forEach((t: ITerm) => {
                let ip1 = {
                  parent: ele['Name'],
                  name: t['Name'],
                  id: t['Id'].replace("/Guid(", "").replace(")/", ""),
                };
                tms.push(ip1);

              });
            });
          });
          tbatch.execute().then(_r => {
            resolve(tms);
          });
        });
      });

With:

interface IPTerm {
  parent?: string;
  id: string;
  name: string;
}

interface ITaxonomyPopulatorState {
  terms: IPTerm[];
}

That works for me. This is a link that may help you: Get all child terms from term group in SharePoint Taxonomy using PnP JS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.