I have a requirement where I need to find the Taxonomy Field assigned to a page property
.
For Example, Language
is a page property and Unity is the group name
assigned to it and Language is the termset
for that property.
Now, I need to find that dynamically to know what group is assigned to the page property using PowerShell.
I have cast it into a taxonomy field and got 2 properties like SspId
and TermSetId
. By using the SspId
, I was able to find the termstore properly, but not the group and the termset.
My Code :
$TaxFldLanguage = $pagesList.Fields.GetByInternalNameOrTitle("Language");
$ctx.Load($TaxFldLanguage);
$ctx.ExecuteQuery();
#casting here
$objTxFieldOffice = [Microsoft.SharePoint.Client.ClientContext].GetMethod("CastTo").MakeGenericMethod([Microsoft.SharePoint.Client.Taxonomy.TaxonomyField]).Invoke($ctx, $TaxFldOffice);
$termStoreID = $objTxFieldOffice.SspId.ToString();
$termSetID = $objTxFieldOffice.TermSetId.ToString();
#find the termstore and the matching termsetID
$termStores = $TaxonomySession.TermStores.GetById($termStoreID);
$ctx.Load($termStores);
$ctx.ExecuteQuery();
if ($termStores) {
$termStore = $termStores[0];
$Groups = $termStore.Groups;
$ctx.Load($termStore);
$ctx.Load($Groups);
$ctx.ExecuteQuery();
foreach ($grp in $Groups) {
#loop through all termsets in all groups and check with the $termSetID
$grpTermsets = $grp.TermSets;
$ctx.Load($grpTermsets);
$ctx.ExecuteQuery();
foreach ($trmset in $grpTermsets) {
Write-Host "Termset: " + $trmset.Name -ForegroundColor Gray;
$terms = $trmset.Terms;
$ctx.Load($terms);
$ctx.ExecuteQuery();
foreach ($trm in $terms) {
Write-Host $trm.Id -ForegroundColor Blue;
if ($trm.Id -eq $termSetID) {
Write-Host "Group: "+ $grp.Name + "Termset:"+ $trmset.Name -ForegroundColor DarkRed;
break;
}
}
}
}
}