Publish Content Type in SharePoint using PowerShell
Content types that we create in one SharePoint site collection should be published from Managed metadata content type hub to make the content type available in all other site collections that consume Managed Metadata Service application.
How to publish from content type hub in SharePoint?
To publish a content type from the content type hub site in SharePoint, browse to your content type hub site collection, navigate to:
- Site Settings >> Site Content Types
- Select the content type that you would like to publish.
- On the content type settings page, click on the “Manage publishing for this content type” link.
- From this page, you can publish a content type by selecting “Publish” and clicking on “OK”.
Also, If you make any changes to an existing published content type in the content type hub site, you should republish the content type for the changes to take effect. Likewise, if you no longer want your content type to be available, you can unpublish the content type. (unpublishing will not remove the content type in use from subscriber sites). All published content types become available in site collections of consuming web applications after the content type hub, and content type subscriber timer jobs run.
How to push changes from Content Type Hub to Site Collections immediately?
Once the content type is published, it may take up to an hour for the subscribing Site Collections to get it. This is internally done by a timer job called “Content Type Subscriber” that is scheduled to run once an hour. To publish a content type and want to see them in your site collections immediately on-demand, go to:
- Central Administration >> Monitoring > Review Job Definitions
- Pick the “Content Type Subscriber” job of your target web application and click on Run now
- Now, your content type should be available for use in subscribed sites.
Publish content type SharePoint using PowerShell
If the content type publishing activity is too frequent and repeating, we can automate it with PowerShell. Let’s publish the content type in SharePoint using PowerShell.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
# Function to publish the content types
Function Publish-ContentType($HubSiteURL, $ContentTypeName)
{
#Get the objects from Parameters
$HubSite = Get-SPSite $HubSiteURL
$ContentType = $HubSite.RootWeb.ContentTypes | Where {$_.Name -eq $ContentTypeName}
#Check if the content type exists
if($ContentType -ne $null)
{
#Create a new content type publisher
$Publisher = New-Object Microsoft.SharePoint.Taxonomy.ContentTypeSync.ContentTypePublisher($HubSite)
#Publish-Unpublish
$Publisher.Publish($ContentType)
Write-Host "Published Content Type $ContentTypeName" -foregroundcolor Green
}
else
{
Write-Host "Content Type $ContentTypeName Not Found!" -ForegroundColor Red
}
}
#Configuration Parameters
$HubSiteURL="https://intranet.crescent.com/CTypeHub"
$ContentTypeName="Sales Proposal v.1"
#call the function to publish content type
Publish-ContentType $HubSiteURL $ContentTypeName
How about Publishing all Content Types from a Content Type Group?
Here is the SharePoint PowerShell to publish content type.
#Configuration Parameters
$HubSiteURL="https://intranet.crescent.com/CTypeHub"
$ContentTypeGroup="Crescent Templates"
#Get the content type hub
$HubSite = Get-SPSite $HubSiteURL
#Publish each content type from the group
$HubSite.RootWeb.ContentTypes | where { $_.Group -match $ContentTypeGroup } | ForEach-Object {
Publish-ContentType $HubSiteURL $_.Name
}
To Trigger the Timer job using PowerShell:
$SubscriberWebAppURL="https://intranet.crescent.com/"
#Run the Content Type Subscriber timer job for a specific Web Application
$CTypeSubscriberTimerJob = Get-SPTimerJob "MetadataSubscriberTimerJob" -WebApplication $SubscriberWebAppURL
$CTypeSubscriberTimerJob.RunNow()
Is there a way to stop the republishing? We accidentally hit the “Refresh all published content type” at the site collection level and we want to stop it. We disabled the content subscriber job in the mean time but getting it out of the queue would be what we need.
Hello Salaudeen,
Thank you for this great script.
I wanted to find out if there is a script to publish the content types for SP online using Powershell?
Thanks,
JS
Unfortunately, No! We can’t publish content types in SharePoint Online using PowerShell as of today.
Hello Salaudeen,
This is really good. Thank you.
I wanted to find out if there is a script to publish ALL the content types and not just by groups.
Thanks,
Ricky
Just remove the “Where” part.
Instead of:
$HubSite.RootWeb.ContentTypes | where { $_.Group -match $ContentTypeGroup } | ForEach-Object {
Publish-ContentType $HubSiteURL $_.Name
}
use:
$HubSite.RootWeb.ContentTypes | ForEach-Object {
Publish-ContentType $HubSiteURL $_.Name
}