{"id":2165,"date":"2016-05-13T13:34:28","date_gmt":"2016-05-13T11:34:28","guid":{"rendered":"http:\/\/www.itidea.nl\/?p=2165"},"modified":"2016-05-12T15:20:06","modified_gmt":"2016-05-12T13:20:06","slug":"gettaxonomysession-cannot-convert-argument-context","status":"publish","type":"post","link":"https:\/\/www.itidea.nl\/index.php\/gettaxonomysession-cannot-convert-argument-context\/","title":{"rendered":"GetTaxonomySession &#8211; cannot convert argument context"},"content":{"rendered":"<p>The other day I was trying to add terms to the site collection level term group using PowerShell CSOM when an issue arose:<\/p>\n<blockquote><p>Cannot convert argument &#8220;context&#8221;, with value: &#8220;Microsoft.SharePoint.Client.ClientContext&#8221;, for &#8220;GetTaxonomySession&#8221;\u00a0to type &#8220;Microsoft.SharePoint.Client.ClientRuntimeContext&#8221;<\/p><\/blockquote>\n<p>Let me explain the situation.<br \/>\nThere is an on-premises SP2013 environment where to connect to with the OfficeDev PnP PowerShell command Connect-SPOnline:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\nConnect-SPOnline -Url &lt;site collection url&gt; -CurrentCredentials\r\n<\/pre>\n<p>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:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$context = Get-SPOContext\r\n$MMS = &#x5B;Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($context)\r\n$context.Load($MMS)\r\n$context.ExecuteQuery()\r\n$termStore = $MMS.GetDefaultSiteCollectionTermStore()\r\n$context.Load($termStore)\r\n$context.ExecuteQuery()\r\n<\/pre>\n<p>When the line with &#8216;GetTaxonomySession&#8217; got executed the exception<\/p>\n<blockquote><p>Cannot convert argument &#8220;context&#8221;, with value: &#8220;Microsoft.SharePoint.Client.ClientContext&#8221;, for &#8220;GetTaxonomySession&#8221;\u00a0to type &#8220;Microsoft.SharePoint.Client.ClientRuntimeContext&#8221;<\/p><\/blockquote>\n<p>was thrown.<\/p>\n<p>After some digging around the way to connect to the environment and retrieved a context was changed to:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)\r\n$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)\r\n$clientContext.Credentials = $credentials\r\n<\/pre>\n<p>After successfully connected to the environment the code to get a taxonomysession and to get to the site collection term store was executed successfully.<\/p>\n<p>Another issue arose when the code was executed to another environment which uses ADFS to connect. The way to connect to the environment doesn&#8217;t work when using ADFS (Connect-SPOnline does), because it needs network credentials. Another difference between the environments is that I&#8217;m a site collection administrator at this environment and the first environment is my development (with all permissions) environment\u2026 So probably it will turn out to be a permission issue\u2026<\/p>\n<p>Back to the initial issue: how to get the site collection term group.<\/p>\n<p>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\u2026<\/p>\n<p>What other options are there to use?<\/p>\n<p>Get-SPOTaxonomySession<\/p>\n<p>The code was changed to:<\/p>\n<p>$context = Get-SPOContext<br \/>\n$session = Get-SPOTaxonomySession<br \/>\n$termStore = $session.GetDefaultSiteCollectionTermStore()<br \/>\n#get the groups<br \/>\n$context.Load($termStore.Groups)<br \/>\n$context.ExecuteQuery()<br \/>\n[\\powershell]<\/p>\n<p>And the exception was gone in both environments while connecting with Connect-SPOnline&#8230;<\/p>\n<p>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.<\/p>\n<h4>Summary<\/h4>\n<p>The call [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession throws the exception<\/p>\n<blockquote><p>Cannot convert argument &#8220;context&#8221;, with value: &#8220;Microsoft.SharePoint.Client.ClientContext&#8221;, for &#8220;GetTaxonomySession&#8221;\u00a0to type &#8220;Microsoft.SharePoint.Client.ClientRuntimeContext&#8221;<\/p><\/blockquote>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8220;context&#8221;, with value: &#8220;Microsoft.SharePoint.Client.ClientContext&#8221;, for &#8220;GetTaxonomySession&#8221;\u00a0to type &#8220;Microsoft.SharePoint.Client.ClientRuntimeContext&#8221; Let me explain the situation. There &#8230; <a class=\"more-link\" href=\"https:\/\/www.itidea.nl\/index.php\/gettaxonomysession-cannot-convert-argument-context\/\">Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[39,22,35],"tags":[37,43,44],"class_list":["post-2165","post","type-post","status-publish","format-standard","hentry","category-office-365","category-powershell","category-sharepoint-2013","tag-office365","tag-powershell","tag-sharepoint-2013"],"_links":{"self":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/2165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/comments?post=2165"}],"version-history":[{"count":2,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/2165\/revisions"}],"predecessor-version":[{"id":2167,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/posts\/2165\/revisions\/2167"}],"wp:attachment":[{"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/media?parent=2165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/categories?post=2165"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itidea.nl\/index.php\/wp-json\/wp\/v2\/tags?post=2165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}