Mise en place un concentrateur de type de contenu avec Powershell

Dans l’article précédent, je vous ai montré [Dans l’article précédent, je vous ai montré][1] par l’interface graphique. Maintenant je passe à la vitesse supérieure en vous montrant comment industrialiser cela en PowerShell.

Configurer le service de métadonnées gérées et le concentrateur de type de contenu

$MmsName = "Managed Metadata Service"
$HubUrl = "http://contenttypehub.contoso.com"
# set or change HubUri on the given Managed Metadata Service
# activate the Content Type Hub Syndication feature on the site collection (9a447926-5937-44cb-857a-d3829301c73b)
Get-SPServiceApplication -Name $MmsName | Set-SPMetadataServiceApplication -HubURI $HubUrl -Confirm:$false
    
# Enable content type syndication and content type Pushdown in Menaged Metadata Service
Get-SPMetadataServiceApplicationProxy -Identity $MmsName | Set-SPMetadataServiceApplicationProxy -ContentTypeSyndicationEnabled -ContentTypePushdownEnabled -Confirm:$false</pre>

<table>
  <tr>
    <td width="614">
      <strong>Astuce: </strong>
    </td>
  </tr>
  
  <tr>
    <td width="614">
      Par la centrale dadministration, il est impossible de modifier lURL du CTH après lavoir renseignée au niveau du MMS. Au besoin vous pouvez le faire avec la commande PowerShell<strong> Set-SPMetadataServiceApplication.</strong>
    </td>
  </tr>
</table>

# Créer des types de contenu {#créer-des-types-de-contenu}

Je vais juste montrer lassociation du modèle de document au type de contenu.

<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-powershell" data-lang="powershell"><span style="color:#66d9ef">Function</span> AddTemplateToCT($ContentType, $TemplateName, $TemplatePath)
{
   $fileMode = <span style="color:#66d9ef">[System.IO.FileMode]</span>::Open
   <span style="color:#66d9ef">try</span>
   {
      <span style="color:#75715e">#upload template</span>
      $fileStream = New-Object <span style="color:#e6db74">&#34;System.IO.FileStream&#34;</span> -ArgumentList $TemplatePath, $fileMode
      $ContentType.ResourceFolder.Files.Add($TemplateName, $fileStream, $true) | Out-Null
      $fileStream.Close()

      <span style="color:#75715e">#set the current document template file to be the document template</span>
      $ContentType.DocumentTemplate = $docTemp.TargetName
      <span style="color:#75715e">#update the content type</span>
      $ContentType.Update($true)
   }
   <span style="color:#66d9ef">finally</span>
   {
      $fileStream.Close()
   }
}&lt;</code></pre></div>

# Publier (ou republier) les types de contenu {#publier-ou-republier-les-types-de-contenu}

<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-powershell" data-lang="powershell">$CTPublish = New-Object Microsoft.SharePoint.Taxonomy.ContentTypeSync.ContentTypePublisher ($HubUrl)
$spWeb = Get-SPWeb $HubUrl
<span style="color:#75715e"># $ctToPublish contains the name of every content type to be published</span>
<span style="color:#66d9ef">foreach</span>($ct <span style="color:#66d9ef">in</span> $ctToPublish)
{
   $SPCT = $spweb.ContentTypes[$ct]
   <span style="color:#66d9ef">if</span>($SPCT <span style="color:#f92672">-ne</span> $null)
   {
      $CTPublish.Publish($SPCT)
   }
}

$spWeb.Dispose()</code></pre></div>

# Exécuter les timer job {#exécuter-les-timer-job}

<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-powershell" data-lang="powershell"><span style="color:#75715e">#Run the Content Type Hub timer job</span>
$ContentTypeHubTimerJob = Get-SPTimerJob <span style="color:#e6db74">&#34;MetadataHubTimerJob&#34;</span>
$ContentTypeHubTimerJob.RunNow()

<span style="color:#75715e">#Run the Content Type Subscriber timer job for a specific Web Application</span>
$ContentTypeSubscriberTimerJob = Get-SPTimerJob <span style="color:#e6db74">&#34;MetadataSubscriberTimerJob&#34;</span> -WebApplication $webappUrl
$ContentTypeSubscriberTimerJob.RunNow()</code></pre></div>

Avec ces quelques lignes pour initier le mouvement, quest ce qui vous empêche encore de mettre en place un content type hub ?

 [1]: https://ghislainfo.wordpress.com/2015/01/05/comment-faciliter-lacces-aux-modeles-de-documents