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.

Advertisements

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

    Like

  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

    Like

    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”);

      Liked by 1 person

Please leave a comment or feedback

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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