Wednesday, December 11, 2019

Export Online SharePoint Term Store Data to CSV Files


#Set the variables
$siteCollectionUrl = "https://TENANT.sharepoint.com/sites/SITE_COLECTION"
$folderPath = "D:\Terms\Export\"
$termGroupName = "GROUP_NAME"

# Check if you have the Online SharePoint Powershell and 
# SharePoint PnP PowerShell Online modules installed or not
Get-Module -Name Microsoft.Online.SharePoint.PowerShell -ListAvailable | Select Name,Version
Get-Module -Name SharePointPnPPowerShellOnline -ListAvailable | Select Name,Version

# Uncomment these lines if not installed already
#Install-Module -Name Microsoft.Online.SharePoint.PowerShell
#Install-Module SharePointPnPPowerShellOnline

# Connect to your site (I am using the WebLogin for the two-factor authentication):
Connect-PnPOnline -Url $siteCollectionUrl -UseWebLogin

# Get the group you want to export its term sets
$termGroup = Get-PnPTermGroup -GroupName $termGroupName -Includes TermSets

# Loop over the Term Sets
foreach ($termSetItem in $termgroup.TermSets)
{    
    write-host "Working on Term Set:"$termSetItem.Name "[" $termSetItem.Id "]"

    # The Collection that will hold the Terms in this Term Set
    $collection = @()

    # Load the Term Set
    $termSet = Get-PnPTermSet -TermGroup $termGroup -Identity $termSetItem.Id -Includes Terms

    # Create the object and add it to the collection
    $termData = new-object PSObject
    $termData | Add-member -membertype NoteProperty -name "Term Set Name" -Value $termSet.Name
    $termData | Add-member -membertype NoteProperty -name "Term Set Description" -Value $termSet.Description
    $termData | Add-member -membertype NoteProperty -name "LCID" -Value ""
    $termData | Add-member -membertype NoteProperty -name "Available for Tagging" -Value "TRUE"
    $termData | Add-member -membertype NoteProperty -name "Level 1 Term" -Value ""
    $termData | Add-member -membertype NoteProperty -name "Level 2 Term" -Value ""
    $termData | Add-member -membertype NoteProperty -name "Level 3 Term" -Value ""
    $collection += $TermData    

    # Loop over the Terms Level 1
    foreach ($termItem1 in $termSet.Terms)
    {
        # Load the Level 1 Term
        $term1 = Get-PnPTerm -TermGroup $termGroup -TermSet $termSetItem.Id -Identity $termItem1.Id -Includes Terms        
        
        # Create the object and add it to the collection
        $term1Data = new-object PSObject
        $term1Data | Add-member -membertype NoteProperty -name "Term Set Name" -Value ""
        $term1Data | Add-member -membertype NoteProperty -name "Term Set Description" -Value ""
        $term1Data | Add-member -membertype NoteProperty -name "LCID" -Value ""
        $term1Data | Add-member -membertype NoteProperty -name "Available for Tagging" -Value "TRUE"
        $term1Data | Add-member -membertype NoteProperty -name "Level 1 Term" -Value $term1.Name
        $term1Data | Add-member -membertype NoteProperty -name "Level 2 Term" -Value ""
        $term1Data | Add-member -membertype NoteProperty -name "Level 3 Term" -Value ""
        $collection += $term1Data

        # Loop over the Terms Level 2
        foreach ($termItem2 in $term1.Terms)
        {
            # Load the Level 2 Term
            $term2 = Get-PnPTerm -TermGroup $termGroup -TermSet $termSetItem.Id -Identity $termItem2.Id -Includes Terms        
        
            # Create the object and add it to the collection
            $term2Data = new-object PSObject
            $term2Data | Add-member -membertype NoteProperty -name "Term Set Name" -Value ""
            $term2Data | Add-member -membertype NoteProperty -name "Term Set Description" -Value ""
            $term2Data | Add-member -membertype NoteProperty -name "LCID" -Value ""
            $term2Data | Add-member -membertype NoteProperty -name "Available for Tagging" -Value "TRUE"
            $term2Data | Add-member -membertype NoteProperty -name "Level 1 Term" -Value $term1.Name
            $term2Data | Add-member -membertype NoteProperty -name "Level 2 Term" -Value ""
            $term2Data | Add-member -membertype NoteProperty -name "Level 3 Term" -Value ""
            $collection += $term2Data

            # Loop over the Terms Level 3
            foreach ($termItem3 in $term2.Terms)
            {
                # Load the Level 3 Term
                $term3 = Get-PnPTerm -TermGroup $termGroup -TermSet $termSetItem.Id -Identity $termItem3.Id -Includes Terms        
        
                # Create the object and add it to the collection
                $term3Data = new-object PSObject
                $term3Data | Add-member -membertype NoteProperty -name "Term Set Name" -Value ""
                $term3Data | Add-member -membertype NoteProperty -name "Term Set Description" -Value ""
                $term3Data | Add-member -membertype NoteProperty -name "LCID" -Value ""
                $term3Data | Add-member -membertype NoteProperty -name "Available for Tagging" -Value "TRUE"
                $term3Data | Add-member -membertype NoteProperty -name "Level 1 Term" -Value $term1.Name
                $term3Data | Add-member -membertype NoteProperty -name "Level 2 Term" -Value $term2.Name
                $term3Data | Add-member -membertype NoteProperty -name "Level 3 Term" -Value $term3.Name
                $collection += $term3Data
            }
        }
    }

    # Build the file path, I am using the Term Set name as the filename
    $filePath = $folderPath + $termSet.Name + ".csv"

    # Write to the file
    $collection | Export-csv $filePath -notypeinformation
}

Blessings of working on large real-world projects

Working on large real-world projects can offer numerous blessings and benefits, both professionally and personally. Here are some of the key...