SharePoint 2013 – PowerShell – Taxonomy through Client Object Model

I tried to get to the my Taxonomy session through the client object model. Well if you Bing (I actually used google. But I’m Microsoft focussed so I thought I better mention Bing here) and go to the Microsoft pages documentation the there isn’t much available. All I found is some empty pages.

So first of all load the dlls ( Make sure that you’ve got these dlls on your client!)

Add-Type -Path “c:\Scripts\Microsoft.SharePoint.Client.dll”
Add-Type -Path “c:\Scripts\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “c:\Scripts\Microsoft.SharePoint.Client.Taxonomy.dll”

The first two are for the SharePoint Client Object Model. The 3rd dll is for the taxonomy stuff.

Then I setup the Client Object Model Context

$ctx = new-object Microsoft.SharePoint.Client.ClientContext($siteUrl)

Then like without the Client Object Model I need to get the session.

$session = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($ctx);
$ctx.Load($session);
$ctx.ExecuteQuery();

Ok, so now I’ve got a taxonomy session

Now I need to get to my TermStores

$termstores = $session.TermStores
$ctx.Load($termstores);
$ctx.ExecuteQuery();

I’m likely only to have one termstore, but just in case.

$termstores |% {
$termstore = $_;
if ($termstore.Name -eq “Managed Metadata Service”)
{
… More to come here …
}
}

Then to complete the above code I got my groups and groupset from the termstore

$groups = $termstore.Groups;
$ctx.Load($groups);
$ctx.ExecuteQuery();

$groupset = $groups.GetByName(“My Company”);
$ctx.Load($groupset);
$ctx.ExecuteQuery();

$termsets = $groupset.TermSets;
$ctx.Load($termsets);
$ctx.ExecuteQuery();

$industrytermSet = $termsets.GetByName(“Industry”);
$ctx.Load($industrytermSet);
$ctx.ExecuteQuery();

And my termset is available.

So now we’re nearly there.

I want to be able to get to my terms in a termset. So just a few more steps to go.

Now I’m getting all my terms ( I’ve only got 22 in this termset so no problem to pick them all up)

$industryterms = $industrytermSet.GetAllTerms()
$ctx.Load($industryterms);
$ctx.ExecuteQuery();

Now to get my term all I need to do is:

$term = $industryterms.GetByName(“Aerospace”)

Hopefully this will help someone else.


Discover more from SharePains

Subscribe to get the latest posts sent to your email.

Avatar of Pieter Veenstra

Is your business still running on paper trails, sprawling Excel files, or ageing Access databases? There's a better way — and I can show you exactly what it looks like. I'm the Technical Director of Vantage 365, a Microsoft solutions consultancy working with clients across the UK, the Netherlands, and worldwide. For over 30 years I've been turning messy, manual business processes into clean, automated systems that save time, reduce errors, and give teams the visibility they need to make better decisions. SharePains is not just any blog run by a Microsoft MVP. Have you ever used Try-Catch in Power Automate? The original post about Try-Catch in Power Automate can still be found on this site, https://sharepains.com/2018/02/07/try-catch-finally-in-power-automate-flow/ Or have you ever used the Pieter’s method to avoid variables and speed up your flows? https://sharepains.com/2020/03/11/pieters-method-for-advanced-in-flows/ You can contact me using contact@sharepains.com

Related Posts

3 thoughts on “SharePoint 2013 – PowerShell – Taxonomy through Client Object Model

  1. Hello! Came across this while trying to figure out why mine isn’t working. I’m trying to do the same but getting a conversion error. Using SDK v16.0.4002.1211.

    Cannot convert argument context with value Microsoft.SharePoint.Client.ClientContext for GetTaxonomySession to type Microsoft.SharePoint.Client.ClientRuntimeContext: Cannot convert the Microsoft.SharePoint.Client.ClientContext value of type Microsoft.SharePoint.Client.ClientContext to type Microsoft.SharePoint.Client.ClientRuntimeContext

  2. Hi Patrick,
    I have seen that error before. Most of the time this is down to dll versions not being right. Can you check if you have multiple versions of the Microsoft.SharePoint.Client dlls on your system? Did you reboot after the SDK installation?
    Pieter

    1. Hi Pieter. Thanks for the reply. Indeed I think it was multiple SDKs installed that was causing the error. Seems odd that Add-Type doesn’t load the right version, despite being explicitly typed. I resolved by using the older LoadWithPartialName way:

      [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client”);
      [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client.Runtime”);
      [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client.Taxonomy”);

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SharePains

Subscribe now to keep reading and get access to the full archive.

Continue reading