2

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

1 Answer 1

1

This is how I got the termstore and termset assigned to the page property.

#load TermStore here
$TaxonomySession = [Microsoft.SharePoint.Client.Taxonomy.TaxonomySession]::GetTaxonomySession($ctx);
$TermStore = $TaxonomySession.GetDefaultSiteCollectionTermStore();
$ctx.Load($TaxonomySession);
$ctx.Load($TermStore);
$ctx.ExecuteQuery();

#get page property
$TaxFldLanguageB = $pagesList.Fields.GetByInternalNameOrTitle("LanguageB");
$ctx.Load($TaxFldLanguageB);
$ctx.ExecuteQuery();

#cast to taxonomy
$objTxFieldOffice = [Microsoft.SharePoint.Client.ClientContext].GetMethod("CastTo").MakeGenericMethod([Microsoft.SharePoint.Client.Taxonomy.TaxonomyField]).Invoke($ctx, $TaxFldLanguageB);

#get termstore and termset id
$termStoreID = $objTxFieldOffice.SspId.ToString();
$termSetID = $objTxFieldOffice.TermSetId.ToString();

$termsetData = $TermStore.GetTermSet($termSetID);
$ctx.Load($termsetData);
$ctx.ExecuteQuery();

$termsData = $termsetData.Terms;
$ctx.Load($termsData);
$ctx.ExecuteQuery();

foreach ($objTerm in $termsData) {
    Write-Host $objTerm.Name " - " $objTerm.Id;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.