Last active
August 29, 2015 14:07
-
-
Save devendrasv/975511ef6c57d55ba1a9 to your computer and use it in GitHub Desktop.
check if Site collection Taxonomy group exists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Credentials to connect to office 365 site collection url | |
$url ="https://velegandla.sharepoint.com/sites/training" | |
$username="[email protected]" | |
$password="yourpassword" | |
$Password = $password |ConvertTo-SecureString -AsPlainText -force | |
Write-Host "Load CSOM libraries" -foregroundcolor black -backgroundcolor yellow | |
Set-Location $PSScriptRoot | |
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll") | |
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll") | |
Add-Type -Path (Resolve-Path "Microsoft.Online.SharePoint.Client.Tenant.dll") | |
Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Taxonomy.dll") | |
Write-Host "CSOM libraries loaded successfully" -foregroundcolor black -backgroundcolor Green | |
Write-Host "authenticate to SharePoint Online site collection $url and get ClientContext object" -foregroundcolor black -backgroundcolor yellow | |
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($url) | |
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password) | |
$Context.Credentials = $credentials | |
$context.RequestTimeOut = 5000 * 60 * 10; | |
$web = $context.Web | |
$site = $context.Site | |
$context.Load($web) | |
$context.Load($site) | |
try | |
{ | |
$context.ExecuteQuery() | |
Write-Host "authenticateed to SharePoint Online site collection $url and get ClientContext object succeefully" -foregroundcolor black -backgroundcolor Green | |
} | |
catch | |
{ | |
Write-Host "Not able to authenticateed to SharePoint Online site collection $_.Exception.Message" -foregroundcolor black -backgroundcolor Red | |
return | |
} | |
#Getting Term store Information | |
function Get-TermStoreInfo | |
{ | |
Write-Host "Loading taxonomy session" -foregroundcolor black -backgroundcolor yellow | |
$session = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($context) | |
$context.Load($session) | |
$context.ExecuteQuery() | |
Write-Host "Loading term stores" -foregroundcolor black -backgroundcolor yellow | |
$termStores = $session.TermStores | |
$context.Load($termStores) | |
try | |
{ | |
$context.ExecuteQuery() | |
$termStore = $termStores[0] | |
Write-Host "Term store with the following id is loaded:" $termStore.Id -foregroundcolor black -backgroundcolor Green | |
} | |
catch | |
{ | |
Write-Host "Error detail while getting term store id" $_.Exception.Message -foregroundcolor black -backgroundcolor Red | |
return | |
} | |
return $termStore | |
} | |
#Check if the site collection taxonomy group exists or not using IsSiteCollectionGroup property of taxonomy group | |
function CheckSiteCollectionTaxonomygroupexists | |
{ | |
$termStore = Get-TermStoreInfo $context | |
Write-Host "Check if the group exists if not available create it" -foregroundcolor black -backgroundcolor yellow | |
$groups = $termStore.Groups | |
$context.Load($groups) | |
try | |
{ | |
$context.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 | |
} | |
$siteCollectionTaxonomyGroup = $false | |
foreach($group in $groups) | |
{ | |
if($group.IsSiteCollectionGroup) | |
{ | |
Write-Host "Site Collection $url Taxonomy group already exists." -foregroundcolor black -backgroundcolor Green | |
$siteCollectionTaxonomyGroup = $true | |
} | |
} | |
if(!$siteCollectionTaxonomyGroup) | |
{ | |
Write-Host "Site Collection $url Taxonomy group doesn't exists." -foregroundcolor black -backgroundcolor RED | |
} | |
} | |
Write-Host "Check whether the Site collection Taxonomy group is exist or not" -foregroundcolor black -backgroundcolor yellow | |
CheckSiteCollectionTaxonomygroupexists | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment