Updating Content Hub Subscribers using PowerShell

While testing some functionality for a client recently I found something interesting when publishing shared content types through the content type hub.

After initial creation of the content types and adding subscribers all seemed to work well. That is all content types were ‘pushed’ down to all the subscribers and each subscriber site could make use of the shared content types.

Once I started making changes that is where things started to get interesting. I found that for each content type hub subscriber, it creates a new timer job for each of the web application subscribers. I also found that each timer jobs only runs hourly by default. As I was working on my local development server and making changes quite regularly, I wanted a way to force updates each time I was making updates.

PowerShell to the rescue!

Here is a PSH cmdlet that I wrote that will force an update of all published content types through the content type hub each time it is run.

It will enumerate through each web application and start the MetadataSubscriberTimerJob.

Too easy!

Please feel free to modify etc as you see fit.

Here is the code:

######################################################

#Update Content Type hub subscribers for all web apps

#set the execution policy
Set-ExecutionPolicy Unrestricted

#Add the required SP2010 snapins
Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null

#Updates the Content Type subscribers for each web application
function Update-ContentHub([string]$url)
{
    #Get the Timer job info    
    $job = Get-SPTimerJob -WebApplication $url | ?{ $_.Name -like "MetadataSubscriberTimerJob"}

    #check that the job is not null
    if ($job -ne $null) 
    {  
        #run the timer job
        $job | Start-SPTimerJob

        #run the admin action
        Start-SPAdminJob -ErrorAction SilentlyContinue    

    }    

}

#get the web applications and update the content type hub subscribers for each web application
Get-SPWebApplication | ForEach-Object { Write-Host "Updating Metadata for site:" $_.Url; Update-ContentHub -url $_.Url }

######################################################