0

I am attempting to write a PowerShell script to extract taxonomy entries from one environment to import them into another.

I'm running into a problem where one of my Term Sets' Terms property is not returning all Term objects it contains until they have been fetched the first time:

code

write-host "first time"
$termset.Terms
write-host "second time"
$termset.Terms

output

[DBG]: PS U:\>>> c
first time


CustomProperties      : {}
IsAvailableForTagging : True
CustomSortOrder       :
IsDeprecated          : False
IsKeyword             : False
IsReused              : False
IsRoot                : True
IsSourceTerm          : True
Labels                : {Microsoft.SharePoint.Taxonomy.Label}
MergedTermIds         : {}
Name                  : CEO
Owner                 : XXX\username
Parent                :
ReusedTerms           : {}
SourceTerm            : Microsoft.SharePoint.Taxonomy.Term
Terms                 : {}
TermSet               : Microsoft.SharePoint.Taxonomy.TermSet
TermSets              : {Divisions}
TermsCount            : 0
CreatedDate           : 26/03/2014 5:02:38 AM
Id                    : 2239cd62-9c68-4abd-af5d-485451ef6751
LastModifiedDate      : 26/03/2014 5:02:38 AM
TermStore             : Microsoft.SharePoint.Taxonomy.TermStore

CustomProperties      : {}
IsAvailableForTagging : True
CustomSortOrder       :
IsDeprecated          : False
IsKeyword             : False
IsReused              : False
IsRoot                : True
IsSourceTerm          : True
Labels                : {Microsoft.SharePoint.Taxonomy.Label}
MergedTermIds         : {}
Name                  : Corporate Support Division
Owner                 : XXX\username
Parent                :
ReusedTerms           : {}
SourceTerm            : Microsoft.SharePoint.Taxonomy.Term
Terms                 : {Risk, Compliance and Commercial Services, Information and Technology Services, Portfolio Management, People and Performance.
                        ..}
TermSet               : Microsoft.SharePoint.Taxonomy.TermSet
TermSets              : {Divisions}
TermsCount            : 6
CreatedDate           : 26/03/2014 5:02:40 AM
Id                    : 5349b60d-fdea-4ebe-ac7c-fd3b5a65f60e
LastModifiedDate      : 26/03/2014 5:02:40 AM
TermStore             : Microsoft.SharePoint.Taxonomy.TermStore

second time
CustomProperties      : {}
IsAvailableForTagging : True
CustomSortOrder       :
IsDeprecated          : False
IsKeyword             : False
IsReused              : False
IsRoot                : True
IsSourceTerm          : True
Labels                : {Microsoft.SharePoint.Taxonomy.Label}
MergedTermIds         : {}
Name                  : CEO
Owner                 : XXX\username
Parent                :
ReusedTerms           : {}
SourceTerm            : Microsoft.SharePoint.Taxonomy.Term
Terms                 : {}
TermSet               : Microsoft.SharePoint.Taxonomy.TermSet
TermSets              : {Divisions}
TermsCount            : 0
CreatedDate           : 26/03/2014 5:02:38 AM
Id                    : 2239cd62-9c68-4abd-af5d-485451ef6751
LastModifiedDate      : 26/03/2014 5:02:38 AM
TermStore             : Microsoft.SharePoint.Taxonomy.TermStore

CustomProperties      : {}
IsAvailableForTagging : True
CustomSortOrder       :
IsDeprecated          : False
IsKeyword             : False
IsReused              : False
IsRoot                : True
IsSourceTerm          : True
Labels                : {Microsoft.SharePoint.Taxonomy.Label}
MergedTermIds         : {}
Name                  : Corporate Support Division
Owner                 : XXX\username
Parent                :
ReusedTerms           : {}
SourceTerm            : Microsoft.SharePoint.Taxonomy.Term
Terms                 : {Risk, Compliance and Commercial Services, Information and Technology Services, Portfolio Management, People and Performance.
                        ..}
TermSet               : Microsoft.SharePoint.Taxonomy.TermSet
TermSets              : {Divisions}
TermsCount            : 6
CreatedDate           : 26/03/2014 5:02:40 AM
Id                    : 5349b60d-fdea-4ebe-ac7c-fd3b5a65f60e
LastModifiedDate      : 26/03/2014 5:02:40 AM
TermStore             : Microsoft.SharePoint.Taxonomy.TermStore

CustomProperties      : {}
IsAvailableForTagging : True
CustomSortOrder       :
IsDeprecated          : False
IsKeyword             : False
IsReused              : False
IsRoot                : True
IsSourceTerm          : True
Labels                : {Microsoft.SharePoint.Taxonomy.Label}
MergedTermIds         : {}
Name                  : Finance Division
Owner                 : XXX\username
Parent                :
ReusedTerms           : {}
SourceTerm            : Microsoft.SharePoint.Taxonomy.Term
Terms                 : {Financial Services, Financial Systems, Taxation and Financial Accounting, Budgets and Management Accounting}
TermSet               : Microsoft.SharePoint.Taxonomy.TermSet
TermSets              : {Divisions}
TermsCount            : 4
CreatedDate           : 26/03/2014 5:02:41 AM
Id                    : eea74cc6-af6e-412b-a72a-02f3ab31d4d7
LastModifiedDate      : 26/03/2014 5:02:41 AM
TermStore             : Microsoft.SharePoint.Taxonomy.TermStore

CustomProperties      : {}
IsAvailableForTagging : True
CustomSortOrder       :
IsDeprecated          : False
IsKeyword             : False
IsReused              : False
IsRoot                : True
IsSourceTerm          : True
Labels                : {Microsoft.SharePoint.Taxonomy.Label}
MergedTermIds         : {}
Name                  : Governance and Secretariat Division
Owner                 : XXX\username
Parent                :
ReusedTerms           : {}
SourceTerm            : Microsoft.SharePoint.Taxonomy.Term
Terms                 : {Strategy, Secretariat, Legal Services, Reviews and Complaints...}
TermSet               : Microsoft.SharePoint.Taxonomy.TermSet
TermSets              : {Divisions}
TermsCount            : 5
CreatedDate           : 26/03/2014 5:02:42 AM
Id                    : 70cafbda-bfd0-4257-8b2d-885cb6b056d5
LastModifiedDate      : 26/03/2014 5:02:42 AM
TermStore             : Microsoft.SharePoint.Taxonomy.TermStore

CustomProperties      : {}
IsAvailableForTagging : True
CustomSortOrder       :
IsDeprecated          : False
IsKeyword             : False
IsReused              : False
IsRoot                : True
IsSourceTerm          : True
Labels                : {Microsoft.SharePoint.Taxonomy.Label}
MergedTermIds         : {}
Name                  : Investment Management Division
Owner                 : XXX\username
Parent                :
ReusedTerms           : {}
SourceTerm            : Microsoft.SharePoint.Taxonomy.Term
Terms                 : {Direct Investments, Property Portfolio, Investment Products}
TermSet               : Microsoft.SharePoint.Taxonomy.TermSet
TermSets              : {Divisions}
TermsCount            : 3
CreatedDate           : 26/03/2014 5:02:44 AM
Id                    : 57739bfa-3f87-43fd-b786-c7be887f7761
LastModifiedDate      : 26/03/2014 5:02:44 AM
TermStore             : Microsoft.SharePoint.Taxonomy.TermStore

CustomProperties      : {}
IsAvailableForTagging : True
CustomSortOrder       :
IsDeprecated          : False
IsKeyword             : False
IsReused              : False
IsRoot                : True
IsSourceTerm          : True
Labels                : {Microsoft.SharePoint.Taxonomy.Label}
MergedTermIds         : {}
Name                  : Operations Division
Owner                 : XXX\username
Parent                :
ReusedTerms           : {}
SourceTerm            : Microsoft.SharePoint.Taxonomy.Term
Terms                 : {Product, Marketing, Member Solutions, Admin Support...}
TermSet               : Microsoft.SharePoint.Taxonomy.TermSet
TermSets              : {Divisions}
TermsCount            : 5
CreatedDate           : 26/03/2014 5:02:45 AM
Id                    : f95283a3-519c-4e23-b54c-36d6f9c2ffcd
LastModifiedDate      : 26/03/2014 5:02:45 AM
TermStore             : Microsoft.SharePoint.Taxonomy.TermStore

It appears to have something to do with loading certain properties of the Terms themselves, as if I filter with $termset.terms|select name it still only gives the first two terms.

here is the full version of my script:

function Dump-Hierarchy($obj, $node)
{
    $lcid = 1033

    if ($obj.GetType().Name -ne $node.Type)
    {
        throw "Dump-Hierarchy: The supplied object's type does not match the supplied node's type."
    }

    switch($node.Type)
    {
        "TaxonomySession"
        {
            foreach ($termstorenode in $node.Subs | Not-Null)
            {
                $termstore = $obj.TermStores|?{$_.Name -eq $termstorenode.Name}
                if ($termstore)
                {
                    Dump-Hierarchy $termstore $termstorenode
                }
                else
                {
                    throw "Dump-Hierarchy: The supplied TaxonomySession object has no TermStore named `"$($termstorenode.Name)`""
                }
            }
        }
        "TermStore"
        {
            foreach ($groupnode in $node.Subs | Not-Null)
            {
                $group = $obj.Groups|?{$_.Name -eq $groupnode.Name}
                if ($group)
                {
                    if ($group.Id -ne $groupnode.Id)
                    {
                        Write-Host -ForegroundColor Yellow "Warning: Group `"$($groupnode.Name)`" has different id."
                    }
                    Dump-Hierarchy $group $groupnode
                }
                else
                {
                    Dump-Hierarchy $obj.CreateGroup($groupnode.Name) $groupnode
                }
            }
            $obj.CommitAll()
        }
        "Group"
        {
            foreach ($termsetnode in $node.Subs | Not-Null)
            {
                $termset = $obj.TermSets|?{$_.Name -eq $termsetnode.Name}
                if ($termset)
                {
                    if ($termset.Id -eq $termsetnode.Id)
                    {
                        Dump-Hierarchy $termset $termsetnode
                        $obj.TermStore.CommitAll()
                    }
                    else
                    {
                        throw "Dump-Hierarchy: The supplied Group object already has a TermSet object with an incorrect guid named `"$($termsetnode.Name)`""
                    }
                }
                else
                {
                    Dump-Hierarchy $obj.CreateTermSet($termsetnode.Name, $termsetnode.Id) $termsetnode
                    $obj.TermStore.CommitAll()
                }
            }
        }
        "TermSet"
        {
            foreach ($termnode in $node.Subs | Not-Null)
            {
                $obj.Terms | Out-Null
                $term = $obj.Terms[$termnode.Name]
                if ($termnode.name -eq "Finance Division")
                {
                    write-host "first time"
                    $obj.Terms
                    write-host "second time"
                    $obj.Terms
                }
                if ($term)
                {
                    if ($term.Id -eq $termnode.Id)
                    {
                        Dump-Hierarchy $term $termnode
                    }
                    else
                    {
                        throw "Dump-Hierarchy: The supplied TermSet object already has a Term object with an incorrect guid named `"$($termnode.Name)`""
                    }
                }
                else
                {
                    $term = $obj.TermStore.GetTerm($termnode.Id)
                    if ($term)
                    {
                        Dump-Hierarchy $obj.ReuseTerm($term, $true) $termnode
                    }
                    else
                    {
                        Dump-Hierarchy $obj.CreateTerm($termnode.Name, $lcid, $termnode.Id) $termnode
                    }
                }
            }
        }
        "Term"
        {
            foreach ($termnode in $node.Subs | Not-Null)
            {
                $obj.Terms | Out-Null
                $term = $obj.Terms[$termnode.Name]
                if ($term)
                {
                    if ($term.Id -eq $termnode.Id)
                    {
                        Dump-Hierarchy $term $termnode
                    }
                    else
                    {
                        throw "Dump-Hierarchy: The supplied TermSet object already has a Term object with an incorrect guid named `"$($termnode.Name)`""
                    }
                }
                else
                {
                    $term = $obj.TermStore.GetTerm($termnode.Id)
                    if ($term)
                    {
                        Dump-Hierarchy $obj.ReuseTerm($term, $true) $termnode
                    }
                    else
                    {
                        Dump-Hierarchy $obj.CreateTerm($termnode.Name, $lcid, $termnode.Id) $termnode
                    }
                }
            }
        }
    }
}
3
  • Do you have an example of your script? – Remko Mar 26 '14 at 6:40
  • sure thing, edited – Nacht Mar 26 '14 at 22:44
  • It's only loads properly after the TermSets property has been loaded – Nacht Mar 26 '14 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.