Skip to content
  • Home
  • About
  • Contact
Search
Close

Stuff For SharePoint

@Ryan_SharePoint

Tag: Term Store

SharePoint 2013 Powershell – Create Sites From Terms Store Data and set Metadata Fields to Term

May 20, 2015 ryanjonhealy1 Comment

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.

Untitled picture

  • 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 –

Untitled picture2

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
}
}

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…

Untitled picture3

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&#8221; p4:type=”q3:string” xmlns:p4=”http://www.w3.org/2001/XMLSchema-instance”>fa007287-1d8a-4f27-a6aa-f4857e7ef871</Value&gt;
</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


$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
}
}

view raw

Create SP Sites based on Term Store.ps1

hosted with ❤ by GitHub

Script to update Managed Metadata Columns AnchorID


$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

view raw

Set AnhorId for Managed Metadata Fields to Taxonomy Term GUID.ps1

hosted with ❤ by GitHub

Advertisement

Posts

  • Create non group attached Modern Team Site collection with Flow – Team site (no Office 365 group)
  • Show Description on Modern SharePoint Document Libraries
  • SharePoint Designer 2013 Workflows – How to get Task Process ID
  • Activate SharePoint site features using CSOM and javascript
  • CSS for InfoPath forms – The best way to style InfoPath forms for Mobile

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 91 other subscribers

Categories

CSOM css Designer InfoPath Javascript MobileUI Modern Office365 Powershell SharePoint SharePoint Framework UI Uncategorized UX Web Services Workflow

Follow me on Twitter

My Tweets
Follow Stuff For SharePoint on WordPress.com
Create a free website or blog at WordPress.com.
Back to top
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Follow Following
    • Stuff For SharePoint
    • Already have a WordPress.com account? Log in now.
    • Stuff For SharePoint
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
 

Loading Comments...