Import termsets menu script

28/01/2012 14:09

#this is only menu script, not really application, we are using SKG.Metadata.exe file to import termsets. Here we only call this function and delete termsets.

#input parameters
cls


#termstore site url
$siteUrl="http://td_sp_ui/sites/test"

#source folder with folders with termsets (all termset files must have name Metadata.xml)
$sourceDir="C:\InstallFolder\Termstore"
$sourceDirSap="C:\InstallFolder\Termstore\Almira"

#avd termsets definition
$avdTermsets=@("Category Cards",
                    "ContactUs",
                    "Business Unit",
                    "HelpDesk",
                    "HelpdeskCountry",
                     "FAQ",
                     "FAQ Type",
                     "Configuration")
$sapTermsets=@("AAC",
                "Currency",
                "Error Codes",
                "Plant",
                "Storage",
                "Organization",
                "Material"
                )  
               
               
#add pssnapin for sharepoint
#add-pssnapin microsoft.sharepoint.powershell              

#option to change current parameters, siteUrl and sourceDir

    write-host "Current settings of termset import parameters"
    write-host "=============================================="
    write-host "Term store site url: " $siteUrl
    write-host "Path to the termset files: "$sourceDir
    write-host "Path to the SAP (Target system dependent) termset files: "$sourceDirSap
    $input=read-host "Do you want to change site url or .xml source path? Y/N"
    if($input -eq "y")
    {
       
            $siteUrl=read-host "Input term store site url (e. g. http://td_sp_ui:1056):"
            #$errorActionPreference="Stop"
            while(($site=get-spsite $siteUrl) -eq $null)
            {
               
                $siteUrl=read-host "Site url doesn't exist. Check the address and try again: "
               
            }   
       
        $sourceDir=read-host "Input path to termset files (e. g. C:\Termstore):"
        $sourceDirSap=read-host "Input path to SAP (Target system dependent)termset files (e. g. C:\Termstore):"
    }


#Check Termstore access by trying to create /delete group and commit changes
$ErrorActionPreference="Stop" #to make sure, our catch catch the exception
try
{
   $site=get-spsite $siteUrl
   $taxonomySession=Get-SPTaxonomySession -site $site
   $termstore=$taxonomySession.Termstores[0]
   $group=$termstore.CreateGroup("TryGroup")
   $termstore.Groups["TryGroup"].Delete()
   $termstore.CommitAll()
}
catch
{
    write-host ""
    write-host "Access to termstore denied. Check if you are term store Owner im Cenral Administration."
    #exit
   
}
finally
{
  $ErrorActionPreference="Continue"
}


function TermsetExistsDelete
{
    param([string]$termsetName,
          [object]$termstore
            )
    #TODO check if terset exists in termstore, if yes, option to delete or try to import, skip
   
    #Group name
    $groupName="avd Group"
    $group=$termstore.Groups[$groupName]
    if($group -ne $null) #if group exists
    {
        if(($termset=$group.Termsets[$termsetName]) -ne $null) #if termset exists
        {
            $input=read-host "Termset " $termsetName " already exists in term store. Do you want to delete it?"
            if($input -eq "y")
            {
                #delete termset
                $termset.Delete()
                $termstore.CommitAll()
                return $true
            }
            else
            {
                #don't delete, don't call import
                return $false
            }
           
           
        }
    }
  
}


function ImportTermsets
{
    param([array]$termsetGroup,
          [string]$sourceDir,
          [object]$termstore)
   
    foreach($avanT in $termsetGroup)
    {
    $termset=$sourceDir+"\"+$avanT+"\Metadata.xml"
    if(!(Test-Path $termset))
        {
            try
            {
           
                TermsetExistsDelete -termsetName $avanT -termstore $termstore
                #.\SKG.Internet.exe $siteUrl $termset "Y"
                write-host '.\SKG.Internet.exe "'$siteUrl '" "' $termset '" "Y"'
            }
             catch
              {
                    write-error $_
               }
 
            }
        else
        {
            $termsetDir=$sourceDir+"\"+$avanT
            write-host "Termset " $avanT " doesn't exist at " $termset
        }
   
    }

}

 


do {
#================ MENU =================
write-host ""
write-host ""
write-host "1. Import all avd termsets"
write-host "2. Import all SAP termsets"
write-host "3. Import all termsets"
write-host "4. Import specific termset"
write-host "Q  Quit"
write-host "==========================="
$input=read-host "Type number of your option or type 'Q' to quit. "
switch($input)
    {
        "1"{
                write-host "--------- Importing avd termsets --------------"    
                ImportTermsets -termsetGroup $avdTermsets -sourceDir $sourceDir -termstore $termstore
            }
          
         "2"{
                write-host "--------- Importing SAP termsets --------------"
                ImportTermsets -termsetGroup $sapTermsets -sourceDir $sourceDirSap -termstore $termstore
            }
         "3"{
                write-host "--------- Importing all termsets --------------"
                ImportTermsets -termsetGroup $avdTermsets -sourceDir $sourceDir -termstore $termstore
                ImportTermsets -termsetGroup $sapTermsets -sourceDir $sourceDirSap -termstore $termstore
               
            }
         "4"{
                write-host "--------- Importing specific termsets --------------"
                #display all termsets
                $tsetCount=$avdTermsets.length+$sapTermsets.length
                for($i=0;$i -le $tsetCount-1; $i++)
                {
                    if($i -lt $avdTermsets.length)
                        {
                           write-host ($i+1) ". " $avdTermsets[$i]  
                           if($i -eq $avdTermsets.length-1)
                           {
                                write-host "-----------------"
                           }
                        }
                       
                     else
                     {
                        write-host ($i+1) ". " $sapTermsets[$i - $avdTermsets.length]
                     }  
                   
                }
                #termset selection
                [int]$inputNumber=read-host "Select the number of termset to import."
               
               
                if($inputNumber -le $avdTermsets.length) #if termset is from avd termsets
                {
                    $index=$inputNumber-1
                    write-host "You have chosen " $avdTermsets[$index]
                    ImportTermsets -termsetGroup $avdTermsets[$index] -sourceDir $sourceDir
                   
                }
               
                else #if termset is from SAP termsets
                {
                    $index=$inputNumber-$avdTermsets.length-1
                    write-host "You have chosen " $sapTermsets[$index]
                    ImportTermsets -termsetGroup $sapTermsets[$index] -sourceDir $sourceDirSap
                }
    
            }    
    }

}while($input -ne "q")
write-host "==========================="
write-host "Enjoy using your terms!"
#$site.Dispose()