Posted on:
Categories: PowerShell;SharePoint
Description:

We have found that as you add more content types to a content type hub it takes longer and longer for the Content Type Subscriber timer job to run. The time that it takes the Content Type Subscriber time job to finish appears to be directly related to the number of items contained in the content type hub. 

At this point​ we have hundreds of content types in our content type hub, and it can take hours for the Content Type Subscriber timer job to complete. In our testing environments, I'm often asked to manually run the Content Type Subscriber job when new content types are added to the content type hub. Our developers will then have to wait until the job completes in order to proceed with their testing. They are often eager to commence their testing, which means that we end up repeatedly clicking on the Timer Job status page in Central administration to get an indication of how much progress the timer job has made.

In order to get around this inconvenience, I wrote the following script which will start the Content Type Subscriber job for a specified web application. The script will send an email to anyone that you specify once the timer job has completed. In our case I typically send the email to myself and the developer who is waiting to test their application.


#Start and Monitor Content Type Subscriber Timer Job
param (
    [string]$webApp 
)


Function StartAndMonitorCTSubscriberJob($webAppUrl,$to){
 Write-Host -foregroundColor Green "Starting Content Type Subscriber Job..."
 Get-SPTimerJob -WebApplication $webAppUrl -Identity "MetadataSubscriberTimerJob" |Start-SPTimerJob

 Start-Sleep -s 20

 $webAppURL = Get-SPWebApplication $webAppUrl

 do {
  $runningJobs = $webAppURL.WebService.RunningJobs |?{$_.JobDefinitionTitle -eq "Content Type Subscriber"} | select JobDefinitionTitle,ServerName,StartTime,PercentageDone,status | Format-Table -autosize
  $runningJobs
  start-sleep -s 30
 }
 until($runningJobs.Count -eq 0)

 send-mailmessage -to $to -from "no-reply@sharepoint.contoso.com” -subject "Content Type Subscriber Job Completed for $webApp" -SmtpServer server name
}
#separate email addresses with a comma 
StartAndMonitorCTSubscriberJob -WebAppUrl $webApp -To “user1@contoso.com”,”user2@contoso.com”​