SharePoint Pals
 | Sign In
How to publish the Content Types in SharePoint 2013 by PowerShell
by Sathish Nadarajan 12 Jan 2015
Author
SharePoint MVP
Visits   
Today  :   12     Total  :    11915

In one of the Article, We saw how to use the Content Type Hub in SharePoint 2013. In that, we saw a manual step to publish the Content Type, so that, it can be used by other WebApplications/Site Collection. In some scenario, there might be frequent changes on the Content Type, during the development process or even for the packaging, it will not be nice to write these manual steps on the Deployment Guide. Instead, these things were done by the PowerShell Script.

Now the sequence of activity is as follows.

1. Publish the Content Types

2. Run the Content Type Hub Timer

3. Run the Content Type Subscriber Timer Jobs – Every web application whichever required.

Now the script is as follows.

 $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
 $LogFile = ".\PublishCTHub-$LogTime.rtf"
 
 
 cls
 
 
 ##================================================================================================
 ## Description	: This script is used to Publish the Content types and run the subscriber timer jobs.
 ## Author		: Sathish Nadarajan
 ## Date			: 10-Dec-2014
 ##================================================================================================
 
 
  
 $Host.UI.RawUI.WindowTitle = "-- Publish Content types --"
 
 $StartDate = Get-Date
 Write-Host -ForegroundColor White "------------------------------------"
 Write-Host -ForegroundColor White "| Publish Content Types |"
 Write-Host -ForegroundColor White "| Started on: $StartDate |"
 Write-Host -ForegroundColor White "------------------------------------"
 
 
 
 #start-transcript $logfile
 
 $ErrorActionPreference = "Stop"
 ######################### Add SharePoint PowerShell Snapin ###############################################
 
 if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) 
 {
     Add-PSSnapin Microsoft.SharePoint.Powershell
 }
 
 ########################### End of Add SharePoint PowerShell Snapin ##################################
 
 ######################## Set Execution Path ################################################
 
 $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
 Set-Location $scriptBase
 
 
 function Publish-ContentTypeHub 
 {
      param
      (
          [parameter(mandatory=$true)][string]$CTHUrl,
          [parameter(mandatory=$true)][string]$Group  
      )
        $site = Get-SPSite $CTHUrl
        if(!($site -eq $null))
      {   #write-host "Republishing Content type" $Group " to make the changes effective" -foregroundcolor Magenta
          $contentTypePublisher = New-Object Microsoft.SharePoint.Taxonomy.ContentTypeSync.ContentTypePublisher($site)
          $site.RootWeb.ContentTypes | ? {$_.Group -match $Group} | % {
          $contentTypePublisher.Publish($_)
          #write-host "Content type" $Group "has been republished successfully ....... Done !" -foregroundcolor Green
         }
      }
 }
 
 
 function StartContentTypeHubTimerJob
 {     
     $job = Get-SPTimerJob | ?{$_.Name -match "MetadataHubTimerJob"}
     if($job -ne $null  )
     {
         $startet = $job.LastRunTime
         Write-Host -ForegroundColor Yellow -NoNewLine "Running"$job.DisplayName"Timer Job."
         Start-SPTimerJob $job
         while (($startet) -eq $job.LastRunTime)
         {
             Write-Host -NoNewLine -ForegroundColor Yellow "."
             Start-Sleep -Seconds 2
         }
         $lastrun = $job.historyentries | select-object -first 1
 
         if($lastrun.status -eq "Succeeded")
         {
             Write-Host -ForegroundColor Green $job.DisplayName"Timer Job has completed.";
         }
 
         else 
         {
             Write-Host -ForegroundColor red $job.DisplayName"Timer Job has Failed. Please take necessary actions and rerun the timer jobs";
             exit
         }
 
     }
 }
 
 
 function StartTimerJob([string]$WebAppUrl) 
 {
     $wa = Get-SPWebApplication $WebAppUrl
     write-host "Starting content type subscriber timer job for the web application " $wa.name -fore yellow
     $Job = Get-SPTimerJob -WebApplication $wa | ?{ $_.Name -like "MetadataSubscriberTimerJob"}
     if($job -ne $null  )
     {
         $startet = $job.LastRunTime
         Write-Host -ForegroundColor Yellow -NoNewLine "Running"$job.DisplayName"Timer Job."
         Start-SPTimerJob $job
         while (($startet) -eq $job.LastRunTime)
         {
             Write-Host -NoNewLine -ForegroundColor Yellow "."
             Start-Sleep -Seconds 2
         }
         $lastrun = $job.historyentries | select-object -first 1
 
         if($lastrun.status -eq "Succeeded")
         {
             Write-Host -ForegroundColor Green $job.DisplayName"Timer Job has completed.";
         }
 
         else 
         {
             Write-Host -ForegroundColor red $job.DisplayName"Timer Job has Failed.";
         }
 
     }
 }
 
 
 
 
 
 #################Republishing Content type#######################################################
 
 write-host "Republishing associated content types" -fore yellow
 
 $RepublishContentTypeCSV = $scriptBase + "\" + "07.PublishCTHub.RepublishContentType.csv"
 
 import-csv $RepublishContentTypeCSV | where {
     Publish-ContentTypeHub $_.SiteUrl $_.ContentTypeGroupName
 }
 
 write-host "Associated content types republished successfully......... Done !" -fore green
 write-host "loading............." -fore Magenta
 sleep(10)
 
 ############################## Start Content type hub timer job##############
 
 StartContentTypeHubTimerJob
 
 ############################### Start Content Subscription Timer Jobs for Each WEbApplications #####################
 
 $WebApplicationDetailsCSV = $scriptBase + "\" + "07.PublishCTHub.WebApplicationDetails.csv"
 
 import-csv $WebApplicationDetailsCSV | where {
     write-host "loading............." -fore Magenta
     StartTimerJob $_.WebAppUrl
 }
 
 #Stop-Transcript 
 

The script is self explanatory.

Download the Script HERE

Happy Coding.

Sathish Nadarajan.

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation