0

I have a requirement where I need to match a string value with all the terms in a given termset ID. My termset contains terms inside terms(recursively) until 5 levels.

My Current Code :

$TermsetData = $TermStore.GetTermSet($TermSetId);
    $ctx.Load($TermsetData);
    $ctx.ExecuteQuery();

    $level1TermsData = $TermsetData.Terms;
    $ctx.Load($level1TermsData);
    $ctx.ExecuteQuery();

    #match the source value with the destination terms and prepare the value string
    $termValuesStringDest = $null;  
    foreach ($objLevel1Term in $level1TermsData) {
        Write-Host $objLevel1Term.Name -ForegroundColor Red;
        if ($sourceProp -match $objLevel1Term.Name) {                                   
            $termValuesStringDest = "-1;#" + $objLevel1Term.Name + "|" + $objLevel1Term.Id;
            return $termValuesStringDest;
        }
    }

is there any way I can make it recursive?

I have tried recursive this way, but it is not exiting the loop. Can anyone let me know how to break this loop and return a value?

function GetTermStoreRecursive() {
    param(
        [Parameter(Mandatory = $false)][Microsoft.SharePoint.Client.Taxonomy.TermCollection]$TermsCollection,
        [Parameter(Mandatory = $true)][Microsoft.SharePoint.Client.ClientContext]$ctx,        
        [Parameter(Mandatory = $true)][string]$sourceProp
    )

    $level1TermsData = $TermsCollection;

    #match the source value with the destination terms and prepare the value string
    $termValuesStringDest = $null;  

    foreach ($objLevel1Term in $level1TermsData) {
        Write-Host $objLevel1Term.Name -ForegroundColor Red;
        if ($sourceProp -eq $objLevel1Term.Name) {
            $termValuesStringDest = "-1;#" + $objLevel1Term.Name + "|" + $objLevel1Term.Id;
            break;
        }
        else
        {
            $subTerms = $objLevel1Term.Terms;
            $ctx.Load($subTerms);
            $ctx.ExecuteQuery();
            GetTermStoreRecursive $subTerms $ctx $sourceProp
        }
    }

    return $termValuesStringDest;
}

1 Answer 1

0

Actually, there is no need for doing that recursively.

I have found out a method which gets all terms in that termstore from here.

$allTermsData = $TermsetData.GetAllTerms();
$ctx.Load($allTermsData);
$ctx.ExecuteQuery();

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.