This is a two series blog in which I will try to cover lots of SharePoint TermSet Metadata operations. The idea is to achieve the following:
Export Terms to CSV
- Existing Terms along with their child Terms,
- Current Guid,
- Available for Tagging option, &
- Deprecate state.
Import Terms from CSV
- Import Terms which will create Terms only when they don’t already exist. The search will be done using the Term Name.
- Can create the Term with the given Guid. This is optional. If you don’t want to use this option, then keep this column blank and the Term will be created using a brand new Guid.
- Deprecate/Un-Deprecate an existing or a new Term. If you have to change this option on a number of pre-existing TermSets then simply update the exported CSV accordingly and then run the import script.
- Modify the availability for Tagging option. If you have to change this option on a number of pre-existing TermSets then simply update the given CSV accordingly and then run the import script.
- Most importantly, it also applies custom sorting. So the Terms and their child terms will exactly appear in the same order as has been mentioned in the processing CSV.
In this post, I’ll only be showing the export operation. The import operation has been defined here, https://realmpksharepoint.wordpress.com/2015/09/23/import-sharepoint-metadata-from-csv-along-with-their-guids-deprecate-state-and-custom-ordering/. Please note that since the generated output will be in CSV format, we’re converting any comma (,) that might be included in the Term name to “,” otherwise the format will get distorted.
Input
The script expects two parameters that needs to be set before executing it. They are:
- $termGrpNm: The name of the TermGroup whose all TermSets will be exported to CSV files respectively.
- $svLoc: File System location where all the CSVs will be dumped.
Here’s the script preview. Download link for the same has been provided in the later segment.
$termGrpNm = "TEST" $svLoc = "C:\Piyush\ChronicleMigration\TermSetOperations\OrderDeprecateTerms\TermFolder" [Byte[]] $amp = 0xEF,0xBC,0x86 # Ampersands are stored as fullwidth ampersands (see http://www.fileformat.info/info/unicode/char/ff06/index.htm) $commaEncd = "," # Text to be used in place of commam as the CSV file's delimiter is ','. Function ReplaceChars($stringName) { return ($stringName.Replace([System.Text.Encoding]::UTF8.GetString($amp), "&").Replace(",", $commaEncd)) } Function Add-Snapin { if ((Get-PSSnapin -Name Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue) -eq $null) { $global:SPSnapinAdded = $true Write-Host "Adding Sharepoint module to PowerShell" -NoNewline Add-PSSnapin Microsoft.Sharepoint.Powershell -ErrorAction Stop Write-Host " - Done." } Write-Host "Adding Microsoft.Sharepoint assembly" -NoNewline Add-Type -AssemblyName "Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" # Disable the above line and enable the line below for SharePoint 2013 # Add-Type -AssemblyName "Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Write-Host " - Done." } Function Remove-Snapin { if ($global:SPSnapinAdded -eq $true) { Write-Host "Removing Sharepoint module from PowerShell" -NoNewline Remove-PSSnapin Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue Write-Host " - Done." } } Function Export-SPTerms { param ( [string]$termGroupName = $(Read-Host -prompt "Please provide the term group name to export"), [string]$saveLocation = $(Read-Host -prompt "Please provide the path of the folder to save the CSV file to") ) if ([IO.Directory]::Exists($saveLocation) -eq $false) { New-Item ($saveLocation) -Type Directory | Out-Null } #Connect to Central Admin $caWebApp = [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]::Local $CAsiteCollectionUrl = $caWebApp.Sites[0].Url $CAsite = Get-SPSite $CAsiteCollectionUrl write-host "Connection made with Central Admin -"$CAsite $taxonomySession = Get-SPTaxonomySession -site $CAsite $taxonomyTermStore = $taxonomySession.TermStores | Select Name $termStore = $taxonomySession.TermStores[$taxonomyTermStore.Name] foreach ($group in $termStore.Groups) { if ($group.Name -eq $termGroupName) { foreach ($termSet in $group.TermSets) { # Remove unsafe file system characters from filename $parsedFilename = [regex]::replace($termSet.Name, "[^a-zA-Z0-9\\-]", "_") $file = New-Object System.IO.StreamWriter($saveLocation + "\termset_" + $parsedFilename + ".csv") # Write out the headers $file.Writeline("Term Set Name,Term Set Description,LCID,Available for Tagging,Term Description,Deprecated,Level 1 Term, Level 2 Term,Level 3 Term,Level 4 Term,Level 5 Term,Level 6 Term,Level 7 Term") try { Export-SPTermSet $termSet.Terms } finally { $file.Flush() $file.Close() } } } } } Function Export-SPTermSet { param ( [Microsoft.SharePoint.Taxonomy.TermCollection]$terms, [int]$level = 1, [string]$previousTerms = "" ) if ($level -ge 1 -or $level -le 7) { if ($terms.Count -gt 0 ) { $termSetName = "" if ($level -eq 1) { $termSetName = """" + (ReplaceChars $terms[0].TermSet.Name) + """" } $terms | ForEach-Object { $currentTerms = $previousTerms + ",""" + (ReplaceChars $_.Name) + """"; $file.Writeline($termSetName + ",""" + $_.TermSet.Description + """" + "," + $_.ID + "," + $_.IsAvailableForTagging + "," + $_.GetDescription() + "," + $_.IsDeprecated + $currentTerms); if ($level -lt 7) { Export-SPTermSet $_.Terms ($level + 1) $currentTerms } } } } } try { Write-Host "Starting export of Metadata Termsets" -ForegroundColor Green $ErrorActionPreference = "Stop" Add-Snapin Export-SPTerms $termGrpNm $svLoc } catch { Write-Host "" Write-Host "Error : " $Error[0] -ForegroundColor Red throw } finally { Remove-Snapin } Write-Host Finished -ForegroundColor Blue
Here’s the link to download the full script.
And here’s the link to download a sample csv file, the format in which the output will be generated.
Key Takeaways
- Exported file will contain the details like TermSet Guid, Available for Tagging and Deprecate state.
- Any comma found in the name will be encoded to ,
- The script defined here is a modified version of this script. The original script does not deal with the export of Term Guids, Deprecate state, & Tagging options, which has been added here.
[…] of the two-series blog. Before proceeding further, here’s the link to the 1st part, https://realmpksharepoint.wordpress.com/2015/06/19/export-sharepoint-metadata-to-csv-along-with-thei…. The objectivity of the entire series has been already defined there so I won’t be […]
LikeLike
Excellent article.
LikeLike
Thanks! 🙂
LikeLike
I have tried this script and there was an error. Please advise
Starting export of Metadata Termsets
Adding Microsoft.Sharepoint assembly – Done.
Error : Cannot index into a null array.
Cannot index into a null array.
At D:\termset\Export-TermSets.ps1:47 char:41
+ $CAsiteCollectionUrl = $caWebApp.Sites[ <<<< 0].Url
+ CategoryInfo : InvalidOperation: (0:Int32) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : NullArray
LikeLike
awesome script . through in single execution 🙂
LikeLiked by 1 person