Forum Discussion

Ian Gemmell's avatar
Ian Gemmell
Copper Contributor
Aug 29, 2016

Term Store Management

Does anybody know if there is a mechanism to do Term Store Management in bulk via import / export rather than via the UI in SharePoint Online?

6 Replies

  • SanthoshB1's avatar
    SanthoshB1
    Bronze Contributor

    You can use this below script for this. You dont need to Export / Import from one to another. This script will automatically copy Term Group from one to another. 

     

    # Change the following to reflect your environments
    
    # 1) Source Site
    $sUrl = "https://<sourcedomain>.sharepoint.com/"
    $sAdmin = "admin@<domain>.onmicrosoft.com"
    $sPwd = "<password>"
    
    # 2) Destination Site
    $dUrl = "https://<destivation>.sharepoint.com/"                                                                                                  
    $dAdmin = "admin@<domain>.onmicrosoft.com"
    $dPwd = "<password>" 
    
    # 3) What Term Group do you want to synchronize?
    $sTermGroupName = "<Termgroupname>"
    
    ## Stop here
    $lcid = "1033"
    
    $sSecurePwd = ConvertTo-SecureString $sPwd -AsPlainText -Force
    $dSecurePwd = ConvertTo-SecureString $dPwd -AsPlainText -Force
    
    # these aren't required for the script to run, but help to develop
    Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    # doh
    Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll"
    
    # connect/authenticate to SharePoint Online and get ClientContext object.. 
    $sCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sUrl)
    $sCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sAdmin, $sSecurePwd)
    $sCtx.Credentials = $sCredentials
    
    $dCtx = New-Object Microsoft.SharePoint.Client.ClientContext($dUrl)
    $dCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($dAdmin, $dSecurePwd)
    $dCtx.Credentials = $dCredentials
    
    $continue = 0
    
    if (!$dCtx.ServerObjectIsNull.Value)
    {
        Write-Host "Connected to DESTINATION SharePoint Online site: " $dCtx.Url "" -ForegroundColor Green
        $dTaxonomySession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($dCtx)
        $dTaxonomySession.UpdateCache()
        $dCtx.Load($dTaxonomySession)
        $dCtx.ExecuteQuery()
        if (!$dTaxonomySession.ServerObjectIsNull)
        {
            Write-Host "Destination Taxonomy session initiated: " $dTaxonomySession.Path.Identity "" -ForegroundColor Green
    
            $dTermStore = $dTaxonomySession.GetDefaultSiteCollectionTermStore()
            $dCtx.Load($dTermStore)
            $dCtx.ExecuteQuery()
    
            if ($dTermStore.IsOnline) 
            {
                Write-Host "...Default Term Store connected:" $dTermStore.Id "" -ForegroundColor Green
                # $termStoreId will be the SspId in the taxonomy column configs
    
                $continue = 1
            }
        }
    }
    
    if (!$sCtx.ServerObjectIsNull.Value -and $continue -eq 1) 
    { 
        Write-Host "Connected to the SOURCE SharePoint Online site: " $sCtx.Url "" -ForegroundColor Green
        
        $sTaxonomySession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($sCtx)
        $sTaxonomySession.UpdateCache()
        $sCtx.Load($sTaxonomySession)
        $sCtx.ExecuteQuery()
        if (!$sTaxonomySession.ServerObjectIsNull)
        {
            Write-Host "Source Taxonomy session initiated: " $sTaxonomySession.Path.Identity "" -ForegroundColor Green
    
            $sTermStore = $sTaxonomySession.GetDefaultSiteCollectionTermStore()
            $sCtx.Load($sTermStore)
            $sCtx.ExecuteQuery()
    
            if ($sTermStore.IsOnline) 
            {
                Write-Host "...Default Term Store connected:" $sTermStore.Id "" -ForegroundColor Green
                # $termStoreId will be the SspId in the taxonomy column configs
                
                $sCtx.Load($sTermStore.Groups)
                $sCtx.ExecuteQuery()
    
                foreach ($sTermGroup in $sTermStore.Groups)
                {
                    if ($sTermGroup.Name -eq $sTermGroupName)
                    {
                        Write-Host ".....Term Group loaded: " $sTermGroup.Name "-" $sTermGroup.Id "" -ForegroundColor Cyan
                        $sCtx.Load($sTermGroup.TermSets)
                        $sCtx.ExecuteQuery()
    
                        #create this group in the destination context
                        $newGroup = $dTermStore.CreateGroup($sTermGroup.Name, $sTermGroup.Id)
                        $dCtx.Load($newGroup)
                        $dCtx.ExecuteQuery()
                        Write-Host ".....Term Group copied to destination:" $newGroup.Name "" -ForegroundColor Magenta
    
                        foreach($sTermSet in $sTermGroup.TermSets)
                        {
                          try{
                            Write-Host ".......Term Set found: " $sTermSet.Name "-" $sTermSet.Id "" -ForegroundColor Cyan
                            $sCtx.Load($sTermSet.Terms)
                            $sCtx.Load($sTermSet.CustomSortOrder)
                            $sCtx.Load($sTermSet.Description)
                            $sCtx.ExecuteQuery()
                            }
                            catch
                                {}
    
                            #create new term set in destination context
                            $newTermSet = $newGroup.CreateTermSet($sTermSet.Name, $sTermSet.Id, $lcid)
                            $newTermSet.CustomSortOrder=$sTermSet.CustomSortOrder
                            $newTermSet.Description=$sTermSet.Description
                            $dCtx.Load($newTermSet)
                            $dCtx.ExecuteQuery()
                            Write-Host ".......Term Set copied to destination:" $newTermSet.Name "" -ForegroundColor Magenta
                                 #create termstore Custom Properties copy 
                                foreach($TScp in $sTermSet.CustomProperties.GetEnumerator()){
                                try{
                                $newTermSet.SetCustomProperty($TScp.Key,$TScp.Value)
                                $dCtx.Load($newTermSet)
                                $dCtx.ExecuteQuery()
                                }
                                catch
                                {}
                               
                                }
    
                            foreach($sTerm in $sTermSet.Terms)
                            {
                                Write-Host ".........Term found: " $sTerm.Name "-" $sTerm.Id $sTerm.CustomProperties "" -ForegroundColor Cyan
                                
                                $sCtx.Load($sTerm.Labels)
                                $sCtx.ExecuteQuery()
                                #create new term in destination context
                                $newTerm = $newTermSet.CreateTerm($sTerm.Name, $lcid, $sTerm.Id)
                                $newterm.SetDescription($sTerm.Description,$lcid)
                                #  $newterm.CreateLabel($sTerm.Labels)
                                $dCtx.Load($newTerm)
                                $dCtx.ExecuteQuery()
                                  #create term Custom Properties  
                                foreach($cp in $sTerm.CustomProperties.GetEnumerator()){
                                try{
                                $newTerm.SetCustomProperty($cp.Key,$cp.Value)
                                $dCtx.Load($newTerm)
                                $dCtx.ExecuteQuery()
                                }
                                catch
                                {}
                               
                                }
                                 foreach($lable in $sTerm.Labels){
                                try{
                                $newterm.CreateLabel($lable.Value,$lcid,$lable.IsDefaultForLanguage)
                                $dCtx.Load($newTerm)
                                $dCtx.ExecuteQuery()
                                }
                                catch
                                {}
                               
                                }
                                Write-Host ".........Term copied to destination:" $newTerm.Name "" -ForegroundColor Magenta
                            }
                        }
                    }
                }
            }
        }
    } 

     

     

  • yes there is it is a little trick: https://absolute-sharepoint.com/2012/06/importing-managed-metadata-termstore-from-csv-using-powershell.html Kr, Paul

Resources