Import and Export term sets manage metadata to CSV – PS script

#Add-PSSnapin Microsoft.SharePoint.PowerShell 
#[Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Taxonomy") 

param
( 
[string] $outPutDir = (Get-Item (".")).FullName,
[string] $siteURL = "http://MyTest:55555", 
[string] $mmsServiceName = "Managed Metadata Service", 
[string] $grpName = "[GroupNameOfTermSet]",
[string] $termSetName = $(throw "termSetName is required"),
[string] $fileName = $(throw "fileName is required")
)

try        
{ 
write-host "Here"           
	$taxonomySite = Get-SPSite $siteURL             
	$taxSession = Get-SPTaxonomySession -site $taxonomySite 
		try            
		{                 
			$termStore = $taxSession.TermStores[$mmsServiceName]; 
			if ($termStore -ne $null)                 
			{                     
				try                    
				{                         
					$termGroup = $termStore.Groups[$grpName]; 
					if ($termGroup -ne $null)                         
					{                             
						try                            
						{ 
							$termSet = $termGroup.TermSets[$termSetName];                                    
							if ($termSet -ne $null)                                 
							{                                     
								[string]$csvDir = "";
								$csvDir = $outPutDir;                                     
								$outPutFile = $csvDir + "\" + "$fileName";                                        
								$sw = New-Object System.IO.StreamWriter($outPutFile);    
								$sw.writeline('"Term Set Name","Term Set Description","LCID","Available","Term Description","Level 1 Term","Level 2 Term","Level 3 Term","Level 4 Term","Level 5 Term","Level 6 Term","Level 7 Term"');                                     
								[Byte[]] $ampersand = 0xEF,0xBC,0x86;                                     
								#$loop = $termGroup.TermSets.Count;                                     
								#$count = 1; 
								if($TermSet.TermsCount -ne 0)                                             
								{                                                 
									$topTermOutput = '"' + $TermSet.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' +  $TermSet.Description + '","' + $TermSet.Languages + '","' +  $TermSet.IsAvailableForTagging + '","';                                                    
									foreach($childTerm in $TermSet.Terms)                                             
									{                                                 
										if($childTerm.TermsCount -ne 0)                                                 
										{                                                     
											#$topTermOutput = '"' + '","' +  $TermSet.Description + '","' + $TermSet.Languages + '","' +  $TermSet.IsAvailableForTagging + '","';                                                      
											$heritage = $topTermOutput + '","' + $childTerm.Description + '","' + $childTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '"';                                                    
											foreach($secondLevelTerm in $childTerm.Terms)                                                     
											{
												if($secondLevelTerm.TermsCount -ne 0)                                                         
												{                                                             
													foreach($thirdLevelTerm in $secondLevelTerm.Terms)                                                             
													{
														if($thirdLevelTerm.TermsCount -ne 0)                                                                 
														{                                                                     
															foreach($fourthLevelTerm in $thirdLevelTerm.Terms)                                                                     
															{                                                                         
																if($fourthLevelTerm.TermsCount -ne 0)                                                                         
																{                                                                             
																	foreach($fifthLevelTerm in $fourthLEvelTerm.Terms)                                                                             
																	{                                                                                 
																		if($fifthLevelTerm.TermsCount -ne 0)                                                                                 
																		{                                                                                     
																			foreach($sixthLevelTerm in $fifthLevelTerm.Terms)                                                                                     
																			{   
																				if($sixthLevelTerm.TermsCount -ne 0)                                                                                         
																				{                                                                                             
																					foreach($seventhLevelTerm in $sixthLevelTerm.Terms)                                                                                             
																					{                                                                                                 
																						$heritage = $topTermOutput + '","' + $childTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $secondLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $thirdLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $fourthLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $fifthLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $sixthLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $seventhLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '"';                                                                                                 
																						$sw.writeline($heritage);                                                                                                                                                                                            
																					}                                                                                         
																				}                                                                                         
																				else                                                                                        
																				{                                                                                             
																					$heritage = $topTermOutput + '","' + $childTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $secondLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $thirdLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $fourthLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $fifthLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $sixthLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '"';                                                                                             
																					$sw.writeline($heritage);                                                                                         
																				}                                                                                     
																			}                                                                                 
																		}                                                                                 
																		else                                                                                
																		{                                                                                     
																			$heritage = $topTermOutput + '","' + $childTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $secondLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $thirdLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $fourthLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $fifthLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '"';                                                                                     
																			$sw.writeline($heritage);                                                                                 
																		} 
																	}                                                                         
																}                                                                         
																else                                                                        
																{                                                                             
																	$heritage = $topTermOutput + '","' + $childTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $secondLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $thirdLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $fourthLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '"';                                                                             
																	$sw.writeline($heritage);                                                                         
																}                                                                     
															}                                                                 
														} 
														else                                                                
														{                                                                     
															$heritage = $topTermOutput + '","' + $childTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $secondLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $thirdLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '"';                                                                     
															$sw.writeline($heritage);                                                                 
														}                                                             
													}                                                         
												} 
												else                                                        
												{                                                             
													$heritage = $topTermOutput + '","' + $childTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '","' + $secondLevelTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '"';                                                             
													$sw.writeline($heritage);                                                         
												}                                                     
											}                                                 
										}
										else                                                
										{                                                      
											$heritage = $topTermOutput + '","' + $childTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '"';                                                      
											$sw.writeline($heritage);                                                 
										}                                             
									}                                         
								}  
								else                                        
								{                                              
									$heritage = $topTermOutput + '","' + $childTerm.Name.Replace([System.Text.Encoding]::UTF8.GetString($ampersand), "&") + '",,,,,,,' ;                                                                                              
									$sw.writeline($heritage);                                         
								}                                     
							}                                 
						} 
						catch                                
						{                                     
							return "Problem occurred while creating the export file."                                
						}                                 
						$sw.close();                                 
						write-host "Your CSV has been created at $outPutFile";                                 
					} 
					else                            
					{                                 
						return "Termset $termSet does not exist in the term store group $term";                             
					}                         
				}                         
				catch [System.Exception]                         
				{                             
					write-host($_.Exception)                             
					"Unable to acquire the termset $termSetName from the term group $grpName"                        
				}                     
			}
			else                    
			{                         
				return "Term store group $grpName does not exist in the term store $mmsServiceName";                     
			}                 
		}                 
		catch                
		{                     
			"Unable to acquire term store group $grpName from $mmsServiceName"                
		}             
	}  
	catch            
	{                 
		"Unable to acquire session for the site $siteURL";             
	}         
finally        
{             
	$ErrorActionPreference = "Continue";         
}

Reference Link : http://sharepoint-talk.blogspot.com/2011/08/exporting-and-importing-term-set-to-csv.html

Advertisements

One thought on “Import and Export term sets manage metadata to CSV – PS script

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s