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
#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
write-host “Created” -ForegroundColor green
The next challenge was s to now make each site use the relevant terms in its document library managed metadata field.
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
<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>
We need to update this AnchorID value to be the term guid as follows
$field.AnchorId = $element.id
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
Script to update Managed Metadata Columns AnchorID