GetTaxonomySession – cannot convert argument context

13 May

The other day I was trying to add terms to the site collection level term group using PowerShell CSOM when an issue arose:

Cannot convert argument “context”, with value: “Microsoft.SharePoint.Client.ClientContext”, for “GetTaxonomySession” to type “Microsoft.SharePoint.Client.ClientRuntimeContext”

Let me explain the situation.
There is an on-premises SP2013 environment where to connect to with the OfficeDev PnP PowerShell command Connect-SPOnline:

Connect-SPOnline -Url <site collection url> -CurrentCredentials

To get to the site collection term group the following code was used to get a taxonomysession and to get to the site collection term store:

$context = Get-SPOContext
$MMS = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($context)
$context.Load($MMS)
$context.ExecuteQuery()
$termStore = $MMS.GetDefaultSiteCollectionTermStore()
$context.Load($termStore)
$context.ExecuteQuery()

When the line with ‘GetTaxonomySession’ got executed the exception

Cannot convert argument “context”, with value: “Microsoft.SharePoint.Client.ClientContext”, for “GetTaxonomySession” to type “Microsoft.SharePoint.Client.ClientRuntimeContext”

was thrown.

After some digging around the way to connect to the environment and retrieved a context was changed to:

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
$clientContext.Credentials = $credentials

After successfully connected to the environment the code to get a taxonomysession and to get to the site collection term store was executed successfully.

Another issue arose when the code was executed to another environment which uses ADFS to connect. The way to connect to the environment doesn’t work when using ADFS (Connect-SPOnline does), because it needs network credentials. Another difference between the environments is that I’m a site collection administrator at this environment and the first environment is my development (with all permissions) environment… So probably it will turn out to be a permission issue…

Back to the initial issue: how to get the site collection term group.

Appearently GetTaxonomySession($context) does need more permissions than I have at the specific environment. Even in the development environment when connection with Connect-SPOnline? Strange…

What other options are there to use?

Get-SPOTaxonomySession

The code was changed to:

$context = Get-SPOContext
$session = Get-SPOTaxonomySession
$termStore = $session.GetDefaultSiteCollectionTermStore()
#get the groups
$context.Load($termStore.Groups)
$context.ExecuteQuery()
[\powershell]

And the exception was gone in both environments while connecting with Connect-SPOnline…

When requesting $termStore.Groups.Count the result is the total number of groups in the term store in the development environment. In the other environment one site collection group and the global groups are returned as expected.

Summary

The call [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession throws the exception

Cannot convert argument “context”, with value: “Microsoft.SharePoint.Client.ClientContext”, for “GetTaxonomySession” to type “Microsoft.SharePoint.Client.ClientRuntimeContext”

when using it in the above context. The issue can be solved by using the statement Get-SPOTaxonomySession from the OfficeDevPnP PowerShell library in combination with the Connect-SPOnline command to connect to the environment.

One Reply to “GetTaxonomySession – cannot convert argument context”

  1. If you run in the context of the SharePoint Online Powershell environment, this doesnt occur for me. If however you run in a native PowerShell session, I can repeat the above

Leave a Reply

Your email address will not be published. Required fields are marked *

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