September 24, 2014

SharePoint: Change Term Set Owner using PowerShell via CSOM

SCRIPT

Param (
    [Parameter(Mandatory=$True)]
    [string]$Url,
    [Parameter(Mandatory=$True)]
    [string]$NewOwner,
    [Parameter(Mandatory=$True)]
    [string]$TermGroupName,
    [Parameter(Mandatory=$True)]
    [string]$TermSetName,
    [Parameter(Mandatory=$True)]
    [string]$AdminUsername
)
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Taxonomy")
$lcid = "1033"
Write-Host "Please enter password for $($Url):"
$pwd = Read-Host -AsSecureString
# connect/authenticate to SharePoint Online and get ClientContext object..
$sCtx = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$sCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminUsername, $pwd)
$sCtx.Credentials = $sCredentials
if (!$sCtx.ServerObjectIsNull.Value)
{
    Write-Host "Connected to SharePoint Online: " $sCtx.Url "" -ForegroundColor Green
    $sTaxonomySession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($sCtx)
    $sTaxonomySession.UpdateCache()
    $sCtx.Load($sTaxonomySession)
    $sCtx.ExecuteQuery()
    if (!$sTaxonomySession.ServerObjectIsNull)
    {
        Write-Host "Taxonomy session: " $sTaxonomySession.Path.Identity "" -ForegroundColor Green
        $sTermStore = $sTaxonomySession.GetDefaultSiteCollectionTermStore()
        $sCtx.Load($sTermStore)
        $sCtx.ExecuteQuery()
        if ($sTermStore.IsOnline)
        {
            Write-Host "Term Store connected:" $sTermStore.Id "" -ForegroundColor Green
            $sCtx.Load($sTermStore.Groups)
            $sCtx.ExecuteQuery()
            foreach ($sTermGroup in $sTermStore.Groups)
            {
                if ($sTermGroup.Name -eq $TermGroupName)
                {
                    Write-Host "Term Group: " $sTermGroup.Name "-" $sTermGroup.Id "" -ForegroundColor Green
                    $termSets = $sTermGroup.TermSets
                    $sCtx.Load($termSets)
                    $sCtx.ExecuteQuery()
                    $termSet = $termSets.GetByName($TermSetName)
                    $sCtx.Load($termSet)
                    $sCtx.ExecuteQuery()
                        Write-Host "Term Set found: " $termSet.Name ", Current Owner: " $termSet.Owner "" -ForegroundColor Green
                        $termSet.Owner = $NewOwner
                         Write-Host "Owner changed to: " $NewOwner "" -ForegroundColor Cyan
                }
            }
            Write-Host "Committing..." -ForegroundColor Cyan
            $sTermStore.CommitAll()
            $sCtx.ExecuteQuery()
            Write-Host "Done!" -ForegroundColor Green
        }
    }
}

USAGE

.\ChangeTermSetOwner.ps1 -Url https://myspsite.sharepoint.com -NewOwner newowner@myspsite.com -TermGroupName MyTermGroup -TermSetName MyTermSet -AdminUsername adminusername@myspsite.com

4 comments:

  1. Hi Jussi,

    I tried to follow through your blog.

    I assign variable below
    $mms = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($ctxP)

    I receive error:
    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"."

    ReplyDelete
  2. I get the same error. Were you able to find what the problem was?

    ReplyDelete
  3. I retried running the script and found no issues - even when replacing $sTaxonomySession with $mms. Perhaps make sure you have latest SharePoint Online Client SDK installed (https://www.microsoft.com/en-us/download/details.aspx?id=42038).

    ReplyDelete
    Replies
    1. Indication of missing Client files is if you don't see this output when script is executing LoadWithPartialName lines at the beginning:
      GAC Version Location
      --- ------- --------
      True v4.0.30319 C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.SharePoint.Client\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.dll
      True v4.0.30319 C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.SharePoint.Client.Runtime\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.Runtime.dll
      True v4.0.30319 C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.SharePoint.Client.Taxonomy\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.Taxonomy.dll

      Delete