2

Have a problem when trying to iterate through termstore in Managed Metadata service application.

Termstore architecture is shown below:

MMS
   Termgroup
         Terset
              Level 1 Term
                    Level 2 Term
                         Level 3 Term
            Level 1 Term
                     Level 2 Term
                         Level 3 Term

 etc

I'm using the following powershell script:

$session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termStore = $session.TermStores[0]
$group = $termStore.Groups["%Termgroup name%"]
$termSet = $group.TermSets["%Termset name%"]

When i use the following command:

$terms = $termSet.Terms
$term = $terms | ?{$_.Name -eq "%Level 3 Term%"}

$term is null because $terms array contains only Level 1 Terms (including all child terms).

If i try following:

$terms = $termSet.GetAllTerms

$terms variable contains all terms (should according to method overview) but i still unable to make a selection of specified term.

So the question is - how can i iterate through all terms in termset and select one (or many, depends on need) term?

Thanks!

3 Answers 3

3

GetAllTerms is a method and not property. It should be used like GetAllTerms() Following is the PowerShell Code which should work:

$siteCollectionUrl = "http://sp2010:90"
$site =new-object Microsoft.SharePoint.SPSite($siteCollectionUrl)
$session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termStore = $session.TermStores[0]
$group = $termStore.Groups["Corporate Taxonomy"]
$termSet = $group.TermSets["Geography"]
$terms = $termSet.GetAllTerms()
foreach ($term in $terms)
{
    if ($term.Name -eq "Durban")
    {
        Write-Host "Drban Found!"
    }
}
0
0

Unfortunately, this only finds for first 'Durban' in the hierarchy. For example, if this were a 3 level hierarchy of state,county,city containing california,placer,rosevile and a illinois,snohomish,roseville, a query for roseville would only find the first roseville(seemingly randomly). To find the proper roseville in the hierarchy, one must fine the first level values, match that to california, then find the terms below california, then find placer, then look for the terms in placer to find the true roseville. Its doable in powershell.

0

Added another iteration based on the above script and it worked for me:

$siteCollectionUrl = "http://xxxx"
$site =new-object Microsoft.SharePoint.SPSite($siteCollectionUrl)
$session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termStore = $session.TermStores[0]
$group = $termStore.Groups["Corporate Taxonomy"]
$termSet = $group.TermSets["Geography"]
$terms = $termset.Terms

foreach($term in $terms){

   $subterms = $terms.Terms

   foreach($term1 in $subterms){

      Write-Host $term1.Name

   }

}

Your Answer

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

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