jeudi 16 mai 2013

SharePoint 2013 Managed Meta data .csv and Setting site navigation to use taxonomy using PowerShell and Custom Property

You can import .csv file using the following powershell function, you can after launch the function with the following parameters:

webApplication URL
CSV file path
Group Term Name

In order to set the managed metadata Navigation you have to set the option in "Navigation" menu.

Param(
 [Parameter(Mandatory=$True,Position=1)]
 [string]$WebAppUrl,
 [Parameter(Mandatory=$True,Position=2)]
 [string]$CSVFilePath,
 [Parameter(Mandatory=$True,Position=3)]
 [string]$TermGroupName
)

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

function ImportTermSet([Microsoft.SharePoint.Taxonomy.TermStore]$store, [string]$groupName, [PSCustomObject]$termSet) { 
   function ImportTerm([Microsoft.SharePoint.Taxonomy.Group]$group,
                       [Microsoft.SharePoint.Taxonomy.TermSet]$set,
                       [Microsoft.SharePoint.Taxonomy.Term]$parent,
                       [string[]]$path) {       
     if ($path.Length -eq 0) {
       return
     }
      elseif ($group -eq $null)
       {
       $group = $store.Groups | where { $_.Name -eq $path[0] }
       if ($group -eq $null)
       {
         $group = $store.CreateGroup($path[0])
       }
      }
      elseif ($set -eq $null)
       {
       $set = $group.TermSets | where { $_.Name -eq $path[0] }
       if ($set -eq $null)
        {
         $set = $group.CreateTermSet($path[0])
        }
        $set.SetCustomProperty("_Sys_Nav_IsNavigationTermSet", "True")
     }
      else
       {
       $node = if ($parent -eq $null) { $set } else { $parent }
       $parent = $node.Terms | where { $_.Name -eq $path[0] }      
       if ($parent -eq $null -and $path -ne $null -and $path[0] -ne $null) {
         $parent = $node.CreateTerm($path[0], 1036)
        #Debut Menu
        if($_.MenuID -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Menu ID", $_.MenuID)
        }
        if($_.Menu_Particulier -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Menu Particulier", $_.Menu_Particulier)
        }
        if($_.Menu_Professionnel -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Menu Professionnel", $_.Menu_Professionnel)
        }
        if($_.SousTitre -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Sous Titre", $_.SousTitre)
        }
        if($_.TitreAccueil -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Titre accueil", $_.TitreAccueil)
        }
        if($_.SousTitreAccueil -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Sous titre accueil", $_.SousTitreAccueil)
        }
        if($_.Picto -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Picto", $_.Picto)
        }
        #Fin Menu
        #Debut colonne
        if($_.ColonneID -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Colonne ID", $_.ColonneID)
        }
        if($_.ClasseTitreColonne -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Classe titre colonne", $_.ClasseTitreColonne)
        }
        if($_.TitreColonne -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Titre colonne", $_.TitreColonne)
        }
        #Fin colonne
        #Debut lien
        if($_.LienID -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Lien ID", $_.LienID)
        }
        if($_.SousTitreLien -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Sous titre lien", $_.SousTitreLien)
        }
        #Fin lien
        #Debut rubrique
        if($_.RubriqueID -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Rubrique ID", $_.RubriqueID)
        }
        if($_.BaliseRubriqueIcon -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Balise rubrique icone", $_.BaliseRubriqueIcon)
        }
        if($_.ClasseIconRubrique -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Classe icone rubrique", $_.ClasseIconRubrique)
        }
        if($_.TitreIcon -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Titre icone", $_.TitreIcon)
        }
        if($_.SousTitreIcon -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Sous titre icone", $_.SousTitreIcon)
        }
        if($_.ClasseLien -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Classe lien", $_.ClasseLien)
        }
        if($_.TitreLien -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Titre lien", $_.TitreLien)
        }
        if($_.EmphaseTitre -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Emphase titre", $_.EmphaseTitre)
        }
        if($_.ParentRubriqueID -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Parent rubrique ID", $_.ParentRubriqueID)
        }
        if($_.ClasseSousTitre -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Classe sous titre", $_.ClasseSousTitre)
        }
        if($_.ClasseParagrapheEnglobantLien -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Classe paragraphe englobant lien", $_.ClasseParagrapheEnglobantLien)
        }
        #Fin rubrique
        if($_.Ordre -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Ordre", $_.Ordre)
        }
        if($_.ClasseCSS -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Classe CSS", $_.ClasseCSS)
        }
        if($_.Visible -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Visible", $_.Visible)
        }
        if($_.Type -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("Type", $_.Type)
        }
        if($_.PlanSite -ne "" -and $parent  -ne $null )
        {
            $parent.SetLocalCustomProperty("PlanSite", $_.PlanSite)
        }
       }
     }

     ImportTerm $group $set $parent $path[1..($path.Length)]
         Commit $store 2>&1 | out-null
   }

   function RemoveTermGroup([Microsoft.SharePoint.Taxonomy.TermStore]$store, [string]$groupName) {
     $group = $store.Groups | where { $_.Name -eq $groupName }
     if ($group -ne $null) {
       $group.TermSets | foreach { $_.Delete() }
       $group.Delete()
       $store.CommitAll()
     }
   }
   function Commit($store) {
     $store.CommitAll()
   }

   $termSetName = $termSet[0]."Term Set Name"
   $termSet | where { $_."Level 1 Term" -ne "" } | foreach {
     $path = @($groupName, $termSetName) + @(for ($i = 1; $i -le 5; $i++) {
       $term = $_."Level $i Term"
         if ($term -eq "") {
           break
         } else {
           $term
         }
       }
     )
     ImportTerm -path $path
     $ErrorActionPreference = "Continue";
   }
   }

$session = [Microsoft.SharePoint.Publishing.Navigation.TaxonomyNavigation]::CreateTaxonomySessionForEdit($web)
$store = $session.TermStores[0]
$termSet = Import-csv $CSVFilePath
ImportTermSet $store $TermGroupName $termSet

$store.CommitAll()

Aucun commentaire :

Enregistrer un commentaire