Term store is an important part of a site which can be used extensively to utilise groups, term sets and terms of it.
A group is a set of term sets that all share common security requirements.
Following function creates a group in term store
Try{
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.Publishing.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll'
}
catch {
Throw "Unable to load SharePoint Client runtime"
}
function Create-NewGroup() {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)][string]$MMSbaseurl,
[Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
[Parameter(Mandatory=$false)][int]$lcid
)
begin{
Write-Host "Preparing for term store...."
try{
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($MMSbaseurl)
}
catch {
Write-Host "Error:-->> Unable to create ClientContext at " $MMSbaseurl
}
try
{
Write-Host "Binding to Term set through managed metadata services for ($MMSbaseurl) "
$Context.Credentials = $credentials
$MMS = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Context)
$Context.Load($MMS)
$Context.ExecuteQuery()
Write-Host "Retrieving Term Stores"
$TermStores = $MMS.TermStores
$Context.Load($TermStores)
$Context.ExecuteQuery()
Write-Host "Binding term store"
$TermStore = $TermStores[0]
$Context.Load($TermStore)
$Context.ExecuteQuery()
Write-Host "Creating new group"
$NewGroup = $TermStore.CreateGroup("New Group", [System.Guid]::NewGuid().toString())
$Context.Load($NewGroup)
$Context.ExecuteQuery()
Write-Host "Group created successfully" -ForegroundColor Cyan
}
catch{
Write-Host "Error -->>>> " $_.Exception.Message -ForegroundColor Red
}
}
end{
$Context.Dispose()
}
}
$credentials = Get-Credential
Create-NewGroup "http://yoursite" $credentials 1033
Term sets can be utilised as a hub of navigation links which can be used in Global and Current navigation of a SharePoint site. Further more apart from navigational, term sets can be utilised to bind it with a managed meta data field. So just in theory I believe in a decent sized project term sets plays an important role, and their creation as well as deployment on production environment becomes crucial.
Below is the CSOM PowerShell function which can be used to create a term set.
#Loading SharePoint client assemblies
Try{
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.Publishing.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll'
}
catch {
Throw "Unable to load SharePoint Client runtime"
}
function Create-TermSet() {
param(
[Parameter(Mandatory=$true)][string]$baseurl,
[Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
[Parameter(Mandatory=$false)][string]$TermSetName,
[Parameter(Mandatory=$false)][int]$lcid
)
begin {
#Getting the context of the site which will enable us to take further actions
try {
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($baseurl)
}
catch {
Write-Output "Error:-->> Unable to create ClientContext at " $baseurl " Base URL : "
Throw "Unable to create ClientContext at " + $baseurl + " Base URL : "
}
try
{
#Now in order to deal with any of the term or term set related things we need to get the taxonomy session of Managed MetaData Services
#
Write-Host "Binding to Term set through Managed MetaData Services for ($baseurl) "
$Context.Credentials = $credentials
$MMS = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Context)
$Context.Load($MMS)
$Context.ExecuteQuery()
#Retrieve Term Stores
#
Write-Host "Retrieving Term Stores"
$TermStores = $MMS.TermStores
$Context.Load($TermStores)
$Context.ExecuteQuery()
#Bind to Term Store
#
Write-Host "Binding term store"
$TermStore = $TermStores[0]
$Context.Load($TermStore)
$Context.ExecuteQuery()
#Now once we get hold of Term Store, we need to decide under which group we want to create a Term Set, so bind to groups
#
Write-Host "Binding term store Groups"
$Groups = $TermStore.Groups
$Context.Load($Groups)
$Context.ExecuteQuery()
}
catch{
Write-Host "Error -->>>> " $_.Exception.Message
Throw "Unable to bind term store"
}
#For a site there is a default site collection group. Here we find it and store it in $siteCollectionGroup
#
Write-Host "Find site collection term store"
#The Site Collection Group has default format of name such as 'Site Collection - yoursite.com'
#Creating the temp default group name using replace function for comparison in later line of code
#If you don't want to use this default group or its not available on your site, you can use some existing one and replace name in below variable, or else create a new group
#
$tempGroupName = $baseurl.Replace("http://","Site Collection - ").Replace(":","-").Replace("/","-").ToLower()
foreach($Group in $Groups) {
if (($Group.IsSiteCollectionGroup -eq $true) -and (($Group.Name.ToLower()) -eq $tempGroupName))
{
$siteCollectionGroup = $Group
Write-Host "Term store found."
break
}
}
try
{
#Load site collection group
if($siteCollectionGroup -ne $null)
{
Write-Host "Bind to site collection group"
$Context.Load($siteCollectionGroup)
$Context.ExecuteQuery()
}
#A check is always important whether a Term Set already exists or not, to avoid error.
#
$TermSetsForChk = $siteCollectionGroup.TermSets
$Context.Load($TermSetsForChk)
$Context.ExecuteQuery()
$ChkTermSet = $TermSetsForChk | Where-Object {$_.Name -eq $TermSetName}
if($ChkTermSet -eq $null)
{
Write-Host "Term set does not exists." -ForegroundColor Cyan
Write-Host "Creating TermSet"
$TermSet = $siteCollectionGroup.CreateTermSet($TermSetName,[System.Guid]::NewGuid().toString(),$lcid)
$Context.Load($TermSet)
$Context.ExecuteQuery()
Write-Host "Term set created successfully" -ForegroundColor Cyan
}
else
{
Write-Host "Term set already exists" -ForegroundColor Cyan
}
}
catch {
Write-Host "Error -->>>> Unable to create term set : " $_.Exception.Message
Throw "Unable to create term set"
}
}
end{
$Context.Dispose()
}
}
#Setting up parameters
#
$baseurl = "http://yoursite.com"
$credentials = Get-Credential
$TermSetName = "Any name"
$lcid = 1033
#Call function
#
Create-TermSet $baseurl $credentials $TermSetName $lcid
Now once term set is created we would like to have terms inside it. A very simple function to create terms inside term set is written below.
#Loading SharePoint client assemblies
#
Try{
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.Publishing.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll'
}
catch {
Throw "Unable to load SharePoint Client runtime"
}
function Create-NewTerm() {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)][string]$MMSbaseurl,
[Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
[Parameter(Mandatory=$true)][string]$GroupName,
[Parameter(Mandatory=$true)][string]$TermSetName,
[Parameter(Mandatory=$false)][int]$lcid
)
begin{
Write-Host "Preparing for term store...." -ForegroundColor Cyan
try{
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($MMSbaseurl)
}
catch {
Write-Host "Error:-->> Unable to create ClientContext at " $MMSbaseurl -ForegroundColor Red
}
try
{
Write-Host "Binding to Term store through managed metadata services for ($MMSbaseurl) " -ForegroundColor Cyan
$Context.Credentials = $credentials
$MMS = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($Context)
$Context.Load($MMS)
$Context.ExecuteQuery()
Write-Host "Retrieving Term Stores" -ForegroundColor Cyan
$TermStores = $MMS.TermStores
$Context.Load($TermStores)
$Context.ExecuteQuery()
Write-Host "Binding term store" -ForegroundColor Cyan
$TermStore = $TermStores[0]
$Context.Load($TermStore)
$Context.ExecuteQuery()
Write-Host "Binding term store Groups" -ForegroundColor Cyan
$Groups = $TermStore.Groups
$Context.Load($Groups)
$Context.ExecuteQuery()
foreach($Group in $Groups) {
if ((($Group.Name.ToLower()) -eq $GroupName.ToLower()))
{
$SelectedGroup = $Group
Write-Host "Group found." -ForegroundColor Cyan
break
}
}
$Context.Load($SelectedGroup)
$Context.ExecuteQuery()
Write-Host "Bind to term set" -ForegroundColor Cyan
$TermSets = $SelectedGroup.TermSets
$Context.Load($TermSets)
$Context.ExecuteQuery()
foreach($TermSet in $TermSets) {
if ((($TermSet.Name.ToLower()) -eq $TermSetName.ToLower()))
{
$SelectedTermSet = $TermSet
Write-Host "Term set found." -ForegroundColor Cyan
break
}
}
$Context.Load($TermSet)
$Context.ExecuteQuery()
#Term creation
#
$NewTerm = $TermSet.CreateTerm("New Term", $lcid, [System.Guid]::NewGuid().toString())
$Context.Load($NewTerm)
$Context.ExecuteQuery()
Write-Host "New term created successfully" -ForegroundColor Cyan
}
catch{
Write-Host "Error -->>>> " $_.Exception.Message -ForegroundColor Red
}
}
end{
$Context.Dispose()
}
}
#Get and Set credentials
$credentials = Get-Credential
#Call Function
#
Create-NewTerm "http:YourSite.com" $credentials "New Group" "New Term Set" 1033