A recent project required a detailed site structure and a set of terms for those sites to use for labeling data. The client had well established metadata and provided a taxonomy hierarchy. They wanted each site to have its set of terms to use in its document library for labelling its data. Our challenge was to script the creation of a site structure based upon this data in the Term Store and then provide these sites with the appropriate term set.
To clarify – each site needed to have a document library with a managed metadata field that uses a particular term set – all based upon the same site template so having an identical structure – the only difference being the site name and the term set to be used by the MM field in its document library.
Our first step was to create a CSV file to import the terms into the term store in the correct format – we chose a groupname “Functions” and loaded it in via the term store management tool with the following structure.
- TermSetName – This will be referenced in the code – consider this the parent term
- TermSetDescription – optional
- LCID- optional
- AvailableForTagging – optional
- TermDescription – Optional
- Level1 – This are the main terms – obviously cannot be blank – these will be the names of the site
- Level2 – These are the terms that will be available to the sites by way of a managed metadata field
- Level3, etc… – Your hierarchy will determine how complete this has to be, ours went down a few levels.
Next we created a site template to be used to provision all the sites. The structure was to be very simple with one document library with a managed metadata field pointing the parent term set.
To save a site as a template be sure to have the site feature “SharePoint Server Publishing” deactivated.
If you do not see the link to save the site as a template open the site in SP designer and select site options from the main page –
Now that we have the terms and the template we can write some powershell code…
The following code connects to the taxonomy service session and reads the term sets in the term store and writes an array with those terms
#Get the Term Store ID
$TaxSession = Get-SPTaxonomySession -Site $SiteCollection
$TermStore = $TaxSession.TermStores[“Managed Metadata Service”] #Change to your service name
$TermStore.Id
#Get the Term Set
$TermStoreGroup = $TermStore.Groups[“Functions”] #Change to your Terms Store sets parent term (TermSetName)
$TermSetList = $TermStoreGroup.TermSets
#create an array
$myArray = $TermSetList.Terms
Now we want to create a site structure to mimic this structure.
The next bit of code creates a site for each term (if it doesn’t exist already)
foreach ($element in $myArray)
{
write-host $element.name -ForegroundColor Magenta #writes the term
$NewSiteUrl = $web + “/” + $element.name
write-host $NewSiteUrl -ForegroundColor green #writes the new sites url
#check if it already exists
$exists = Get-SPWeb -Identity $NewSiteUrl -ErrorAction SilentlyContinue | Select-Object -Property Exists -ErrorAction SilentlyContinue
if($exists -eq $null)
{
#create the new site
$newweb = New-SPWeb -Url $NewSiteUrl -Name $element.name -AddToTopNav -UseParentTopNav
#apply your template
$newweb.ApplyWebTemplate($template.Name)
write-host “Created” -ForegroundColor green
}
}
To do this we need to cycle through each site, find its doc library and its Managed Metadata field and at the same time cycle through the terms in level 1 – we then set the anchor point of the field to the correct term set so that the child terms appear as choices in the document library.
So for our Human Resources site the Field should set like so…
By Exploring the schema of the managed metdata field you can determine the AnchorId
<Property>
<Name>AnchorId</Name>
<Value xmlns:q3=”http://www.w3.org/2001/XMLSchema” p4:type=”q3:string” xmlns:p4=”http://www.w3.org/2001/XMLSchema-instance”>fa007287-1d8a-4f27-a6aa-f4857e7ef871</Value>
</Property>
We need to update this AnchorID value to be the term guid as follows
$field.AnchorId = $element.id
$field.Update()
See the full code below for how to find the field and the relevant term GUID
Script to Read Term Store and Create Sites
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
$web = get-spweb http://sp2013/sites/Root #Change to your site URL | |
$template = $web.GetAvailableWebTemplates(1033) | Where-Object {$_.Title -eq “TemplateSite”} #Change to your template name | |
$SiteCollection = Get-SPSite http://sp2013/sites/Root #Change to your site URL | |
#Get the Term Store ID | |
$TaxSession = Get-SPTaxonomySession –Site $SiteCollection | |
$TermStore = $TaxSession.TermStores[“Managed Metadata Service”] #Change to your service name | |
$TermStore.Id | |
#Get the Term Set | |
$TermStoreGroup = $TermStore.Groups[“Functions”] #Change to your Terms Store group name -the main parent term | |
$TermSetList = $TermStoreGroup.TermSets | |
#create an array | |
$myArray = $TermSetList.Terms | |
foreach ($element in $myArray) | |
{ | |
write-host $element.name –ForegroundColor Magenta #writes the term | |
$NewSiteUrl = $web + "/" + $element.name | |
write-host $NewSiteUrl –ForegroundColor green #writes the new sites url | |
#check if it already exists | |
$exists = Get-SPWeb –Identity $NewSiteUrl –ErrorAction SilentlyContinue | Select-Object –Property Exists –ErrorAction SilentlyContinue | |
if($exists -eq $null) | |
{ | |
#create the new site | |
$newweb = New-SPWeb –Url $NewSiteUrl –Name $element.name –AddToTopNav –UseParentTopNav | |
#apply your template | |
$newweb.ApplyWebTemplate($template.Name) | |
write-host "Created" –ForegroundColor green | |
} | |
} |
Script to update Managed Metadata Columns AnchorID
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
$siteColl = Get-SPSite –Identity "http://sp2013/sites/EDMS" | |
$spSite = [Microsoft.SharePoint.SPSite] ($siteColl) | |
$TaxSession = Get-SPTaxonomySession –Site $SiteCollection | |
$TermStore = $TaxSession.TermStores[“Managed Metadata Service”] #Change to your service name | |
$TermStore.Id | |
#Get the Term Set | |
$TermStoreGroup = $TermStore.Groups[“Functions”] #Change to your Terms Store group name -the main parent term | |
$TermSetList = $TermStoreGroup.TermSets | |
#create an array | |
$myArray = $TermSetList.Terms | |
if($spSite -ne $null) | |
{ | |
foreach($subWeb in $spSite.AllWebs) | |
{ | |
if($subWeb -ne $null) | |
{ | |
Write-Host "Subsite : " $subWeb.Name + " – " + $subWeb.Url | |
$spListColl = $subweb.Lists | |
foreach($list in $spListColl) | |
{ | |
if($list.Title -eq "Documents") #Rename to your doc library | |
{ | |
write-host $list.Title –ForegroundColor Red | |
foreach ($field in $list.Fields) #Get all fields the library | |
{ | |
if($field.staticName -eq "DocumentFunction") #Rename to your Managed Metadata field | |
{ | |
#Write-Host $fielD " | " $field.schemaxml -ForegroundColor Green #Uncomment if you want to see the schema for the field | |
#Write-Host $field.AnchorId -ForegroundColor green #Uncomment if you want to see the existing AnchorId | |
foreach ($element in $myArray) | |
{ | |
if ($subweb.Title -eq $element.name) #finds the term that matches the sitename | |
{ | |
Write-Host $subweb.Title " will be updated" –ForegroundColor green | |
$field.AnchorId = $element.id #elementid is the guid of the term – set this as the AnchorId | |
$field.Update() | |
} | |
} | |
} | |
} | |
} | |
} | |
$subWeb.Dispose() | |
} | |
else | |
{ | |
Echo $subWeb "does not exist" | |
} | |
} | |
$spSite.Dispose() | |
} | |
else | |
{ | |
Echo $siteURL "does not exist, check the site collection url" | |
} | |
Echo Finish | |
Awesome
LikeLike