Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanjonhealy/895138c0b0e5592a4575 to your computer and use it in GitHub Desktop.
Save ryanjonhealy/895138c0b0e5592a4575 to your computer and use it in GitHub Desktop.
Set AnhorId for Managed Metadata Fields to Taxonomy Term GUID
$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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment