Office 365 / SharePoint Online - Resolve An Exception While Retrieving The TaxonomySession Object

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:
  1. #Add Type for Taxonomy  
  2. Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"  
  3.  
  4. #Variables  
  5. $url = "https://prashamsabadra1.sharepoint.com/sites/Publishing/" // This is my Office 365 developer account  
  6.   
  7. $userName =”” # Your site user name  
  8. $password =”” # Password  
  9.  
  10. # convert password into secured string  
  11. $securedpw = ConvertTo-SecureString $password -AsPlainText -Force  
  12.  
  13. # creating the context object  
  14. $context = New-Object Microsoft.SharePoint.Client.ClientContext($url)  
  15.  
  16. # Assigning credentials  
  17. $context.Credentials = $credentials  
  18.  
  19. # Retrieving the taxonomy session  
  20. $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,
  1. "Microsoft.SharePoint.Client.ClientRuntimeContext"  
  2. "Microsoft.SharePoint.Client.ClientRuntimeContext": "Cannot convert the  
  3. "Microsoft.SharePoint.Client.ClientContext" value of type  
  4. "Microsoft.SharePoint.Client.ClientContext" to type  
  5. "Microsoft.SharePoint.Client.ClientRuntimeContext"."  
  6. At line:1 char:1  
  7. + $session =  
  8. [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySe ...  
  9. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  10. ~~~  
  11. + CategoryInfo : NotSpecified: (:) [], MethodException  
  12. + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument   

Exception
                     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,

Client
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.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now