Last active
August 29, 2015 14:05
-
-
Save devendrasv/1ec5b10bbf829fa65237 to your computer and use it in GitHub Desktop.
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 tenenat admin url | |
$url ="https://velegandla-admin.sharepoint.com" | |
$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 Tenant site $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 Tenant site $url and get ClientContext object succeefully" -foregroundcolor black -backgroundcolor Green | |
} | |
catch | |
{ | |
Write-Host "Not able to authenticateed to SharePoint Online $_.Exception.Message" -foregroundcolor black -backgroundcolor Red | |
return | |
} | |
function provision-Term($context, $termSet, $label, $lcid) | |
{ | |
$terms = $termSet.Terms | |
$context.Load($terms) | |
$context.ExecuteQuery() | |
$term = $terms | Where-Object {$_.Name -eq $label} | |
if($term) | |
{ | |
Write-Host "Term" $label "already exists." -foregroundcolor black -backgroundcolor Blue | |
} | |
else | |
{ | |
Write-Host "Creating term " $label -foregroundcolor black -backgroundcolor yellow | |
$term = $termSet.CreateTerm($label, $lcid, [System.Guid]::NewGuid()) | |
try | |
{ | |
$context.ExecuteQuery() | |
Write-Host "Term" $label "Created successfully" -foregroundcolor black -backgroundcolor Green | |
} | |
catch | |
{ | |
Write-Host "Error while creating Term" $label $_.Exception.Message -foregroundcolor black -backgroundcolor Red | |
return | |
} | |
} | |
} | |
function provision-TermSet($context, $group, $termSetXml, $lcid) | |
{ | |
$termSets = $group.TermSets | |
$context.Load($termSets) | |
$context.ExecuteQuery() | |
$termSet = $termSets | Where-Object {$_.Name -eq $termSetXml.Name} | |
if($termSet) | |
{ | |
Write-Host "Termset" $termSetXml.Name "already exists." -foregroundcolor black -backgroundcolor Blue | |
$termSet = $group.TermSets.GetByName($termSetXml.Name) | |
$context.Load($termSet) | |
$context.ExecuteQuery() | |
} | |
else | |
{ | |
Write-Host "Creating term set" $termSetXml.Name -foregroundcolor black -backgroundcolor yellow | |
$termSet = $group.CreateTermSet($termSetXml.Name, $termSetXml.Id, $lcid) | |
try | |
{ | |
$context.ExecuteQuery() | |
Write-Host "Term set " $termSetXml.Name "Created successfully" -foregroundcolor black -backgroundcolor Green | |
} | |
catch | |
{ | |
Write-Host "Error while creating Term set" $termSetXml.Name $_.Exception.Message -foregroundcolor black -backgroundcolor Red | |
return | |
} | |
} | |
$termSetXml.Term | ForEach-Object { provision-Term $context $termSet $_.Name $lcid } | |
} | |
#Getting Term store Information | |
function Get-TermStoreInfo | |
{ | |
Write-Host "Loading taxonomy session" -foregroundcolor black -backgroundcolor yellow | |
$session = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($context) | |
$session.UpdateCache(); | |
$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] | |
$context.Load($termStore) | |
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 | |
} | |
#Provisioning the Terms | |
function Provision-managedmetadata($context) | |
{ | |
# Redaing Data from manage metadata.xml file | |
$xmlFilePath = "$PSScriptRoot\managedmetadata.xml" | |
Write-Host "Load managedmetadata.xml file for creating terms" -foregroundcolor Green | |
[xml]$xmlContent = (Get-Content $xmlFilePath) | |
if (-not $xmlContent) | |
{ | |
Write-Host "managedmetadata.xml was not loaded successfully." -foregroundcolor Red | |
return | |
} | |
$termStore = Get-TermStoreInfo $context | |
Write-Host "Getting lis of groups from term store" -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 | |
} | |
Write-Host "Check whether the group is exist in the term store" -foregroundcolor black -backgroundcolor yellow | |
$group = $groups | Where-Object {$_.Name -eq $xmlContent.Group.Name} | |
if ($group) | |
{ | |
Write-Host "Group" $xmlContent.Group.Name "already exists." -foregroundcolor black -backgroundcolor Blue | |
$group = $TermStore.Groups.GetByName($xmlContent.Group.Name) | |
$context.Load($group) | |
$context.ExecuteQuery() | |
} | |
else | |
{ | |
Write-Host "Creating group" $xmlContent.Group.Name -foregroundcolor black -backgroundcolor yellow | |
$group = $termStore.CreateGroup($xmlContent.Group.Name, $xmlContent.Group.Id) | |
try | |
{ | |
$context.ExecuteQuery() | |
Write-Host "Group" $xmlContent.Group.Name "Created successfully" -foregroundcolor black -backgroundcolor Green | |
} | |
catch | |
{ | |
Write-Host "Error while creating Group" $xmlContent.Group.Name $_.Exception.Message -foregroundcolor black -backgroundcolor Red | |
return | |
} | |
} | |
$xmlContent.Group.TermSet | | |
ForEach-Object { provision-TermSet $context $group $_ $termStore.DefaultLanguage } | |
} | |
#Provision group, termset and terms based on managemetadata.xml configuration file | |
Write-Host "Provisioning manage metadata started" -foregroundcolor black -backgroundcolor yellow | |
Provision-managedmetadata ($context) | |
Write-Host "Provisioning manage metadata Completed" -foregroundcolor black -backgroundcolor green |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment