1

I've a termstore with a lot of entries and in treeview. I've to export it to import then in an other site.

I've tried this script but i loose the tree and i can't import it, everything is on the same level

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$url=Read-Host "Entrez l'url du site sharepoint"
$siteCollection = Get-SPSite $url
$web = Get-SPWeb($url)
$termSet = Get-SPTaxonomySession -Site $url
$termStore = $termSet.TermStores["Managed Metadata Service"]
#récupère le term set du groupe Finance
$termGroup = $termStore.Groups["Finance"]

# File and Directory Location
$dirLocation = "C:\tmp\Terms\"

#New-Item ($dirLocation) -Type Directory | Out-Null
$file = New-Object System.IO.StreamWriter(($dirLocation) + "Terms.csv")
#"Term Name,Id,Owner,CreatedDate,LastModifiedDate" | Out-File $path
[Byte[]] $amp = 0xEF,0xBC,0x86

# CSV headers
$file.Writeline("Term Name,Id,Owner,CreatedDate,LastModifiedDate")


foreach ($termGroups in $termGroup.TermSets)
{
    foreach ($term in $termGroups.GetAllTerms()) 
    {
        #getNode $termSets

            [Byte[]] $amp = 0xEF,0xBC,0x86
            $file.Writeline("""" + $term.Name.Replace([System.Text.Encoding]::UTF8.GetString($amp), "&") + """" + "," + $term.Id + "," + $term.Owner + "," + $term.CreatedDate + "," + $term.LastModifiedDate)
            #$term.Name+ "," + $term.Id + "," + $term.Owner + "," + $term.CreatedDate + "," + $term.LastModifiedDate | Out-File $path - Append

    }
}
$file.Flush()
$file.Close()

1 Answer 1

1

Here is what i did to have a treeview. Certainly not the best method but a working solution

        Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$url=Read-Host "Entrez l'url du site sharepoint"
$siteCollection = Get-SPSite $url
$web = Get-SPWeb($url)
$termSet = Get-SPTaxonomySession -Site $url
$termStore = $termSet.TermStores["Managed Metadata Service"]
#récupère le term set du groupe Finance
$termGroup = $termStore.Groups["Categories"]

# File and Directory Location
$dirLocation = "C:\tmp\Terms\"

#New-Item ($dirLocation) -Type Directory | Out-Null
$file = New-Object System.IO.StreamWriter(($dirLocation) + "Terms.csv")
#"Term Name,Id,Owner,CreatedDate,LastModifiedDate" | Out-File $path
[Byte[]] $amp = 0xEF,0xBC,0x86

# CSV headers
$file.Writeline("Term Set Name,Term Set Description, LCID, Available for tagging,Parent1,Parent2,Parent3,Parent4,Parent5,Parent6,Id,Owner,CreatedDate,LastModifiedDate")


foreach ($termGroups in $termGroup.TermSets)
{
    foreach ($term in $termGroups.GetAllTerms()) 
    {
        #getNode $termSets
        $i = 0
        $parent = ""
        $termParent = $term.Parent
        while($i -le 6)
        {

            if($termParent.Id -ne $null)
            {
                $parent += """"+$termParent.Name.Replace([System.Text.Encoding]::UTF8.GetString($amp), "&")+""""
            }
            else
            {
                break
            }
            $parent += ","
            $termParent = $termParent.Parent
            $i++
        }
        $test = $parent.split(",")
        [array]::Reverse($test)
        $parent = $test -join ','
        $parent += """"+$term.Name.Replace([System.Text.Encoding]::UTF8.GetString($amp), "&")+""""
        $cpt = $test.Length
        while($cpt -le 6)
        {
            $parent += ","
            $cpt++
        }

            [Byte[]] $amp = 0xEF,0xBC,0x86
            $file.Writeline("""" + $term.Name.Replace([System.Text.Encoding]::UTF8.GetString($amp), "&") + """" + "," +"""" + $term.Name.Replace([System.Text.Encoding]::UTF8.GetString($amp), "&") + """" + ",,TRUE," + $term.LastModifiedDate+$parent + $parent + "," + $term.Id + "," + $term.Owner + "," + $term.CreatedDate )
    }
}
$file.Flush()
$file.Close()

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.