September 24, 2014

SharePoint: Replacing owners of Term Sets and Terms using PowerShell and CSOM

SCRIPT

Param (
    [Parameter(Mandatory=$True)]
    [string]$Url,
    [Parameter(Mandatory=$True)]
     [string]$OldOwner,
    [Parameter(Mandatory=$True)]
    [string]$NewOwner,
    [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")

## Stop here
$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)
            {
                Write-Host "Term Group: " $sTermGroup.Name "-" $sTermGroup.Id "" -ForegroundColor Green
                $termSets = $sTermGroup.TermSets
                $sCtx.Load($termSets)
                $sCtx.ExecuteQuery()

                foreach($termSet in $termSets)
                {
                    Write-Host "Term Set found: " $termSet.Name ", Current Owner: " $termSet.Owner "" -ForegroundColor Green

                    If($termSet.Owner -match $OldOwner)
                    {
                        Write-Host "Replacing old termset owner!" -ForegroundColor Yellow
                        $termSet.Owner = $NewOwner
                    }

                    $terms = $termSet.Terms
                    $sCtx.Load($terms)
                    $sCtx.ExecuteQuery()
                    foreach($term in $terms)
                    {
                        Write-Host "Term found: " $term.Name ", Current Owner: " $term.Owner "" -ForegroundColor Green

                        If($term.Owner -match $OldOwner)
                        {
                            Write-Host "Replacing old term owner!" -ForegroundColor Yellow
                            $term.Owner = $NewOwner
                        }
                    }
                }               
            }

            #####
            Write-Host "Rolling back..." -ForegroundColor Cyan
            $sTermStore.RollbackAll()
            #Write-Host "Committing..." -ForegroundColor Cyan
            #$sTermStore.CommitAll()
            #####

            $sTermStore.UpdateCache()

            Write-Host "Done!" -ForegroundColor Green
        }
    }
}

 

USAGE

You need to uncommend the CommitAll command at the end, and comment the RollbackAll command in order for the script to store changes in database. Script will loop through all term groups and sets and terms and change owner of term sets and terms.

.\ReplaceTermSetOwner.ps1 -Url https://myspsite.sharepoint.com –OldOwner oldowner@myspsite.com -NewOwner newowner@myspsite.com -AdminUsername adminusername@myspsite.com

No comments:

Post a Comment