Here's an exception which you can face while retrieving the TaxonomySession object. Also, I have provided the solution below.
Office 365 / SharePoint Online - Exception while retrieving the TaxonomySession object -
Cannot convert argument "context", with value: "Microsoft.SharePoint.Client.ClientContext", for "GetTaxonomySession" to type "Microsoft.SharePoint.Client.ClientRuntimeContext"
Background
We have our SharePoint online site and we have created a few TermSets. Now, we need to send those TermSets for approval to the respective department lead. We could easily import the TermSets from the .csv file but unfortunately there is no direct way to export the TermSets. One possible option is PowerShell script.
So, we have written a PowerShell script (I’ll share the complete PowerShell script in a new article).
Here's the code:
- #Add Type for Taxonomy
- Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
-
- #Variables
- $url = "https://prashamsabadra1.sharepoint.com/sites/Publishing/" // This is my Office 365 developer account
-
- $userName =”” # Your site user name
- $password =”” # Password
-
- # convert password into secured string
- $securedpw = ConvertTo-SecureString $password -AsPlainText -Force
-
- # creating the context object
- $context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
-
- # Assigning credentials
- $context.Credentials = $credentials
-
- # Retrieving the taxonomy session
- $taxonomysession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($context)
As we are retrieving the taxonomy session object, exception is thrown.
Exception: Cannot convert argument "context", with value:"Microsoft.SharePoint.Client.ClientContext", for "GetTaxonomySession" to type,
- "Microsoft.SharePoint.Client.ClientRuntimeContext"
- "Microsoft.SharePoint.Client.ClientRuntimeContext": "Cannot convert the
- "Microsoft.SharePoint.Client.ClientContext" value of type
- "Microsoft.SharePoint.Client.ClientContext" to type
- "Microsoft.SharePoint.Client.ClientRuntimeContext"."
- At line:1 char:1
- + $session =
- [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySe ...
- + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ~~~
- + CategoryInfo : NotSpecified: (:) [], MethodException
- + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
Figure 1: Exception while retrieving the TaxonomySession object
Solution: After long time of google search I got a clue that on my laptop there are two versions of “
SharePoint Client Components” installed as,
Figure 2: Two versions of "SharePoint Client Components" are installed
As soon as I uninstalled the old version 15.0.4711.1001,I restarted the management shell and it started working.