September 13, 2018 O365 / SharePoint Online – Import Term Store Terms using CSOM & PowerShell SharePoint Dotnet PowerShell Looking for a solution to import Term Store Terms using CSOM & PowerShell in O365 / SharePoint Online? Here you go…. Note: Modify the XML term attributes and PS script logic as per your requirement XML Config File Structure PowerShell Script try { Write-Host "Load XML config file" -foregroundcolor black -backgroundcolor yellow Set-Location $xdoc = [xml] (get-content "C:\Joseph\Code\SPRIDER\PS\TermStore\config.xml") $Url = $xdoc.Tenant.Admin.site $Admin = $xdoc.Tenant.Admin.username $Pwd = $xdoc.Tenant.Admin.password $SecurePwd = $Pwd ConvertTo-SecureString -AsPlainText -force Write-Host "Global variables loaded succeefully" -foregroundcolor black -backgroundcolor Green } catch { Write-Host "Error : $_.Exception.Message" -foregroundcolor black -backgroundcolor Red return } try { Write-Host "Load CSOM DLLs" -foregroundcolor black -backgroundcolor yellow Set-Location Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll" Write-Host "CSOM DLLs loaded succeefully" -foregroundcolor black -backgroundcolor Green } catch { Write-Host "Error : $_.Exception.Message" -foregroundcolor black -backgroundcolor Red return } function Recursive($inputerm,$inputNode) { Foreach ($InnerTermNode in $inputNode.term) { $childterms = $inputerm.Terms $Ctx.Load($childterms) $Ctx.ExecuteQuery() $childterm = $childterms Where-Object { $_.Name -eq $InnerTermNode.name } if($childterm) { Write-Host "Term" $InnerTermNode.name "already exists." -foregroundcolor black -backgroundcolor yellow } else { Write-Host "Creating term " $InnerTermNode.name -foregroundcolor black -backgroundcolor yellow $childterm = $inputerm.CreateTerm($InnerTermNode.name, 1033, [System.Guid]::NewGuid()) try { $Ctx.ExecuteQuery() Write-Host "Term" $childterm.name "Created successfully" -foregroundcolor black -backgroundcolor Green } catch { Write-Host "Error while creating Term" $InnerTermNode.name $_.Exception.Message -foregroundcolor black -backgroundcolor Red return } } if($InnerTermNode.ChildNodes.Count -gt 0) { Recursive $childterm $InnerTermNode } } } try { Write-Host "Authenticate tenant site $url and get ClientContext object" -foregroundcolor black -backgroundcolor yellow $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Url) $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Admin, $SecurePwd) $Ctx.Credentials = $Credentials if (!$Ctx.ServerObjectIsNull.Value) { Write-Host "Connected to SharePoint Online site: " $Ctx.Url "" -foregroundcolor black -backgroundcolor Green $TaxonomySession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Ctx) $TaxonomySession.UpdateCache() $Ctx.Load($TaxonomySession) $Ctx.ExecuteQuery() Write-Host "Taxonomy session initiated: " $TaxonomySession.Path.Identity "" -foregroundcolor black -backgroundcolor Green $termStore = $TaxonomySession.GetDefaultSiteCollectionTermStore() $Ctx.Load($termStore) $Ctx.ExecuteQuery() Foreach ($GroupName in $xdoc.Tenant.TermStore.group) { Write-Host "Getting list of groups from term store" -foregroundcolor black -backgroundcolor yellow $groups = $termStore.Groups $Ctx.Load($groups) $Ctx.ExecuteQuery() try { $Ctx.ExecuteQuery() Write-Host "List of groups from term store loaded successfully" -foregroundcolor black -backgroundcolor Green } catch { Write-Host "Error while getting list of groups from term store" $_.Exception.Message -foregroundcolor black -backgroundcolor Red return } Write-Host "Check whether the group is exist in the term store" -foregroundcolor black -backgroundcolor yellow $group = $groups Where-Object { $_.Name -eq $GroupName.name } if ($group) { Write-Host "Group" $GroupName.name "already exists." -foregroundcolor black -backgroundcolor yellow $group = $termStore.Groups.GetByName($GroupName.name) $Ctx.Load($group) $Ctx.ExecuteQuery() } else { Write-Host "Creating group" $GroupName.name -foregroundcolor black -backgroundcolor yellow $group = $termStore.CreateGroup($GroupName.name,[System.Guid]::NewGuid().toString()) try { $Ctx.ExecuteQuery() Write-Host "Group" $GroupName.name "Created successfully" -foregroundcolor black -backgroundcolor Green } catch { Write-Host "Error while creating Group" $GroupName.name $_.Exception.Message -foregroundcolor black -backgroundcolor Red return } } Foreach ($TermSetNode in $GroupName.termset) { $termSets = $group.TermSets $Ctx.Load($termSets) $Ctx.ExecuteQuery() $termSet = $termSets Where-Object { $_.Name -eq $TermSetNode.name } if($termSet) { Write-Host "Termset" $TermSetNode.name "already exists." -foregroundcolor black -backgroundcolor yellow $termSet = $group.TermSets.GetByName($TermSetNode.name) $Ctx.Load($termSet) $Ctx.ExecuteQuery() } else { Write-Host "Creating term set" $TermSetNode.name -foregroundcolor black -backgroundcolor yellow $termSet = $group.CreateTermSet($TermSetNode.name,[System.Guid]::NewGuid().toString(),1033) try { $Ctx.ExecuteQuery() Write-Host "Term set " $TermSetNode.name "Created successfully" -foregroundcolor black -backgroundcolor Green } catch { Write-Host "Error while creating Term set" $TermSetNode.name $_.Exception.Message -foregroundcolor black -backgroundcolor Red return } } Foreach ($TermNode in $TermSetNode.term) { $terms = $termSet.Terms $Ctx.Load($terms) $Ctx.ExecuteQuery() $term = $terms Where-Object { $_.Name -eq $TermNode.name } if($term) { Write-Host "Term" $TermNode.name "already exists." -foregroundcolor black -backgroundcolor yellow } else { Write-Host "Creating term " $TermNode.name -foregroundcolor black -backgroundcolor yellow $term = $termSet.CreateTerm($TermNode.name, 1033, [System.Guid]::NewGuid()) try { $Ctx.ExecuteQuery() Write-Host "Term" $TermNode.name "Created successfully" -foregroundcolor black -backgroundcolor Green } catch { Write-Host "Error while creating Term" $TermNode.name $_.Exception.Message -foregroundcolor black -backgroundcolor Red return } } if($TermNode.ChildNodes.Count -gt 0) { Recursive $term $TermNode } } } } } } catch { Write-Host "Error : $_.Exception.Message" -foregroundcolor black -backgroundcolor Red return }
September 13, 2018 O365 / SharePoint Online – Apply Theme – CSOM & PowerShell Sharepoint Dotnet Powershell
September 13, 2018 O365 / SharePoint Online – Load And Install App using CSOM & PowerShell Sharepoint Dotnet Powershell