Terms are the individual entries with a term set.
You can load a collection of terms through a term set or term store.
import {
taxonomy,
ITermStore,
ITerms,
ILabelMatchInfo,
ITerm,
ITermData
} from "@pnp/sp-taxonomy";
const store: ITermStore = await taxonomy.termStores.getByName("Taxonomy_v5o/SbcTE2cegwO2dtAN9l==");
const labelMatchInfo: ILabelMatchInfo = {
TermLabel: "My Label",
TrimUnavailable: true,
};
const terms: ITerms = store.getTerms(labelMatchInfo);
// get term instances merged with data
const terms2: (ITermData & ITerm)[] = await store.getTerms(labelMatchInfo).get();
const terms3: ITerms = store.getTermSetById("0ba6845c-1468-4ec5-a5a8-718f1fb05431").terms;
// get terms merged with data from a term set
const terms4: (ITerm & ITermData)[] = store.getTermSetById("0ba6845c-1468-4ec5-a5a8-718f1fb05431").terms.get();
You can get a single term a variety of ways as shown below. The "best" way will be determined by what information is available to do the lookup but ultimately will result in the same end product.
import {
taxonomy,
ITermStore,
ITerms,
ILabelMatchInfo,
ITerm,
ITermData
} from "@pnp/sp-taxonomy";
const store: ITermStore = await taxonomy.termStores.getByName("Taxonomy_v5o/SbcTE2cegwO2dtAN9l==");
// get a single term by id
const term: ITerm = store.getTermById("0ba6845c-1468-4ec5-a5a8-718f1fb05431");
// get single get merged with data
const term2: ITerm = store.getTermById("0ba6845c-1468-4ec5-a5a8-718f1fb05431").get();
// use select to choose which fields to return
const term3: ITerm = store.getTermById("0ba6845c-1468-4ec5-a5a8-718f1fb05431").select("Name").get();
// get a term from a term set
const term4: ITerm = store.getTermSetById("0ba6845c-1468-4ec5-a5a8-718f1fb05431").getTermById("0ba6845c-1468-4ec5-a5a8-718f1fb05431");
Accesses the labels collection for this term
import { taxonomy, ITermStore, ITerm, ILabels } from "@pnp/sp-taxonomy";
const term: ITerm = <from one of the above methods>;
const labels: ILabels = term.labels;
// labels merged with data
const labelsWithData = term.labels.get();
Creates a new label for this Term
import { taxonomy, ITermStore, ITerm, ILabelData, ILabel } from "@pnp/sp-taxonomy";
const term: ITerm = <from one of the above methods>;
const label: ILabelData & ILabel = term.createLabel("label text", 1031);
// optionally specify this is the default label
const label2: ILabelData & ILabel = term.createLabel("label text", 1031, true);
Sets the deprecation flag on a term
import { ITerm } from "@pnp/sp-taxonomy";
const term: ITerm = <from one of the above methods>;
await term.deprecate(true);
Loads the term data
import { ITerm, ITermData } from "@pnp/sp-taxonomy";
const term: ITerm = <from one of the above methods>;
// load term instance merged with data
const term2: ITerm & ITermData = await term.get();
Sets the description
import { ITerm } from "@pnp/sp-taxonomy";
const term: ITerm = <from one of the above methods>;
// load term instance merged with data
const description = await term.getDescription(1031);
Sets the description
import { ITerm } from "@pnp/sp-taxonomy";
const term: ITerm = <from one of the above methods>;
// load term instance merged with data
await term.setDescription("the description", 1031);
Sets a custom property on this term
import { ITerm } from "@pnp/sp-taxonomy";
const term: ITerm = <from one of the above methods>;
// load term instance merged with data
await term.setLocalCustomProperty("name", "value");
Added in 1.2.8
Adds a child term to an existing term instance.
import { ITerm } from "@pnp/sp-taxonomy";
const parentTerm: ITerm = <from one of the above methods>;
await parentTerm.addTerm("child 1", 1033);
await parentTerm.addTerm("child 2", 1033);