Easiest way to read ULS log error with Correlation ID
Simply run this in Powershell and replace the GUID with the one presented
Easiest way to read ULS log error with Correlation ID
Simply run this in Powershell and replace the GUID with the one presented
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.
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
https://gist.github.com/ryanjonhealy/44a7b4fa126bf4649ae5
Script to update Managed Metadata Columns AnchorID