All Articles

Getting a Term using a Custom Property Value with PnP Core

Logo

Objective

A nice feature of SharePoint Managed Metadata is the possibility to store custom key-value pairs as “Shared custom properties” on terms.

TermCustomProperty

Using the PnP Core SDK they are also easy to add programatically:

// Add a term set
var termSet = await group
    .Sets
    .AddAsync("PnPSet1", "Set description");

// Add a term
var newTerm = await termSet
    .Terms
    .AddAsync("T1", "Description in English");

// Add a custom property with key and value
newTerm.AddProperty(
    "property1", 
    "value1"
);

// Commit changes
await newTerm.UpdateAsync();

I used this feature a lot to create hierarchical structures, e.g. for tree like site collection navigation. Each site collection url is stored as a custom property on the corresponding term.

Simple as that.

The concept is so generic that it opens up a lot of other possibilities, e.g. the key-value pairs do not have to be unique, so it can also be used for categories or classification, etc.

Retrieving Terms using custom properties

A new feature of the PnP Core SDK comes with PnP Core SDK v1.8:

Add ITermSet.GetTermsByCustomProperty method to retrieve a list of terms with a certain custom property key and value

The new method ITermSet.GetTermsByCustomProperty( key, value ) gets a list of all ITerms containing a certain custom property key and value.

// load the terms by custom property
var IList<ITerm> = await termSet
    .GetTermsByCustomProperty(
        "property1", 
        "value1"
     );

There is a section in Working with taxonomy: advanced concepts giving a little bit of context around that topic.

Slick 💪


Photo in Metadata by Ave Calvar Martinez