0

I'm trying to export the terms from the term store using PowerShell but I am receiving following error:

This is the directory where you need to export the term set as a .csv file. 
Please ensure that this folder exists before trying to export the term set. 

Here is script I am using::

$outPutDir = "c:\success" 
#This should be the url of the Central Administration Site of the farm from where we would like to export the term set. 
$siteURL = "http://ussecavdspdwk27:2010"  
#This is the name of the Metadata Service Instance. e.g. "Managed Metadata Service" 
$mmsServiceName = "Managed Metadata Service "  
#This is the group name where the term set exists. 
#In case the group does not exists, the script will create a new group with this name under the term store. 
$grpName = "EandYBusiness"  
#This is the term set name which we are going to export. 
$termSetName = "General Business Taxonomy"  
#This is the name of the csv file which will contain the exported term set data. 
$fileName = "Export.csv"  
try        
{             
  $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";         
}                                                                                          
  • Works like a charm for me - it's probably your folder that's missing like Vedran says. – tarjeieo Jan 26 '12 at 7:30
1

Check if c:\success folder exist.

You can ensure folder with this snippet:

 if ((Test-Path C:\success) -eq $false)
 {
     [IO.Directory]::CreateDirectory("C:\success") 
 }
0

This may help:

$outputfile = "test.txt";

$taxonomySite = "http://test.url.com";

$taxonomySession = Get-SPTaxonomySession -site $taxonomySite $termStore = $taxonomySession.TermStores["Managed Metadata Service"];

function exportTermStore($termStoreGroupName, $termSetName, $termParentName, $termName, $termLabelName) { $log = $termStoreGroupName + "," + $termSetName + "," + $termParentName + "," + $termName + "," + $termLabelName ; $log | out-file $outputfile -append;
}

function processTerms($terms,$termStoreGroupName,$termSetName,$termParentName) {

$terms |% {
    $termName = $_.Name;
    $termLabels = $_.Labels;
    $termLabels |% {
        $termLabelName = $_.Value;

        if ( $termLabelName -eq $termName ) {
            exportTermStore $termStoreGroupName $termSetName $termParentName $termName "";
        }
        else {
            exportTermStore $termStoreGroupName $termSetName $termParentName $termName $termLabelName;
        }

    }
    if ( $_.Terms.Count -ne 0) {
        $termPervName = $termParentName + "," + $termName ;
        write-host $termPervName;
        processTerms $_.Terms $termStoreGroupName $termSetName $termPervName ;
    }
}

}

exportTermStore "Group Name" "TermSet Name" "Parent Term Name" "Term Name" "Label Name";

$termStoreGroup = $termStore.Groups["Test"]; $termStoreGroupName = $termStoreGroup.Name;

$termStoreGroup.TermSets |% { $termSetName = $.Name; $terms = $.Terms; processTerms $terms $termStoreGroupName $termSetName $termSetName; }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.