Pnpjs: Taxonomy: Retrieving term set hierarchy like old spContext.load

Created on 24 Oct 2018  路  4Comments  路  Source: pnp/pnpjs

Category

  • [ ] Enhancement
  • [ ] Bug
  • [x] Question
  • [ ] Documentation gap/issue

Version

Please specify what version of the library you are using: [1.2.3]

Please specify what version(s) of SharePoint you are targeting: [Online]

Question

I'm currently using the taxonomy library of PNPJS. We have a hierarchy of terms under a term set. I'm able to retrieve the terms for the term set via the termSet.terms.get() method, but it returns every single term instead of only the terms under that term set. The data is returned in a flat array. Is there a way to iterate through a term set hierarchically like the old way of loading terms via spContext.load (https://www.c-sharpcorner.com/article/how-to-read-sharepoint-termstore-managed-metadata-in-sharepoint-framework-spfx/)? I'm using modern ES2015 JavaScript instead of TypeScript.

documentation details needed question

Most helpful comment

@patrick-rodgers @simonagren Thank you both for the code examples. I'll try out these algorithms and will comment back. Thanks.

All 4 comments

Please see this issue discussion and let us know if that gets you what you need. It has code example that I think does what you are after.

@rmngh I just yesterday experimentet some with this aswell, here is another example where you:

  1. Get all the root terms as parents.
  2. Loop through all parents and get all the children with corresponding ParentId.
  3. Loop trough recursively and check if there's addidtional children at all levels.
  public async setupTax() {
    const store = await taxonomy.termStores.getByName('Taxonomy_BXNhZFumwYDfQvlT5Br52A==').get();
    const termset: any = await store.getTermSetById('10e3b8d1-edef-48ce-82f4-d184c5cd49b2').get();
    const terms = await termset.terms.select('Id', 'Parent', 'PathOfTerm', 'IsRoot').get();

    let newTerms: any = terms;
    newTerms = terms.map(term => {
      term.Id = this.cleanGuid(term.Id);
      term['PathDepth'] = term.PathOfTerm.split(';').length;
      term.TermSet = { Id: this.cleanGuid(termset.Id), Name: termset.Name };

      if (term["Parent"]) {

        term.ParentId = this.cleanGuid(term["Parent"].Id);
      }
      return term;
    });

    // RootNodes
    const parents = newTerms.filter(t => t.IsRoot);
    // Children of RootNodes
    const children = newTerms.filter(t => t.IsRoot === false);
    // Check if has children
    parents.forEach(parentTerm => {
      this._checkIfChildren(children, parentTerm);
    })

    return parents;
  }


private _checkIfChildren(items, term) {
    const children = [];
    // Loop through and check if parentId equal to parent.Id
    items.forEach(i => {
      if (i.ParentId == term.Id) {
        // Found a child, push
        children.push(i);
        // Remove this term from items and recurr
        this._checkIfChildren(items.filter(item => item !== i), i);
      }
    })
    if (children.length > 0) {
      term.Children = children;
    }
  }

public cleanGuid(guid: string): string {
    if (guid !== undefined) {
      return guid.replace('/Guid(', '').replace('/', '').replace(')', '');
    } else {
      return '';
    }
  }

@patrick-rodgers @simonagren Thank you both for the code examples. I'll try out these algorithms and will comment back. Thanks.

This worked! Thanks guys.

Was this page helpful?
0 / 5 - 0 ratings