Learn more about SQL Server tools

mssqltips logo
 

Tutorials          DBA          Dev          BI          Career          Categories          Webcasts          Whitepapers          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Webcasts

DBA    Dev    BI    Categories

Next Webcast - Simple SQL Server Reporting - Click Here to Register
 

Automating Content Type Publishing with PowerShell


By:   |   Read Comments   |   Related Tips: > Sharepoint

Problem

In SharePoint 2010, the Managed Metadata Service provides the ability to publish content types from a central location called a hub. In my tip "Publishing Content Types with the Managed Metadata Service", I demonstrated how to configure content type publishing through the SharePoint User Interface. In this companion tip, I will demonstrate how to automate the configuration of the Managed Metadata Service hub, and the publishing of content types through PowerShell. 

Solution

The Managed Metadata Service in SharePoint 2010 is a new service application that provides the ability to publish content types across all site collections within all web applications that are connected to the service.

Step 1 - Set the Managed Metadata Service Hub Location

Each instance of a Managed Metadata Service application can optionally be configured with a content type hub. A hub is an associated site collection that is used to publish content types that can be consumed through a web application service connection. Any kind of site collection (Blank, Team) template can be used as a content type hub, but only a single site collection can be configured as the hub for each instance of a Managed Metadata Service application.

$HubUrl = "http://sharepoint/system/metadatahub"
$ServiceName = "Managed Metadata Services"

Get-SPServiceApplication | ForEach-Object `
{
    if ($_.DisplayName -eq $ServiceName)
    {
Set-SPMetadataServiceApplication -Identity $_ `
-HubURI $HubUrl
    }
}

Since the display name of each service application is unique, in the code snippet above, I search for the display name of the Managed Metadata Service application instance I want to update, and then update the hub location with the URL of a site collection that will be the associated content type hub used for publishing.

Step 2 - Enable Content Type Syndication on the Managed Metadata Service Connection

Web applications use service connections to subscribe and consume functionality provided by a service application. Publishing content types to web applications is an optional feature of the Managed Metadata Service. Before web applications can consume published content types, it must be enabled on the Managed Metadata Service Connection.

$HubUrl = "http://sharepoint/system/metadatahub"
$ServiceName = "Managed Metadata Services"

Get-SPServiceApplicationProxy | ForEach-Object `
{
    if ($_.DisplayName -eq $ServiceName)
    {
Set-SPMetadataServiceApplicationProxy -Identity $_ `
-ContentTypeSyndicationEnabled `
-ContentTypePushdownEnabled `
-Confirm:$false
    }
}

Similar to Step #1, first we need to search for the Managed Metadata Service Connection that matches the display name of the specific service to update, then enable content type syndication to support publishing content types from the hub.

Step 3 - Publish a Content Type

Content Types can only be published from a content type hub. When configuring the location of a content type hub on the Managed Metadata Service, SharePoint automatically activates a "Content Type Syndication Hub" feature on the site collection. This feature is responsible for enabling the custom menu links for managing content type publishing, and must be enabled to publish content types.

$HubUrl = "http://sharepoint/system/metadatahub"
$HubSite = Get-SPSite $HubUrl

if (![Microsoft.SharePoint.Taxonomy.ContentTypeSync.ContentTypePublisher]::IsContentTypeSharingEnabled($HubSite))
{
    Enable-SpFeature -Identity 9a447926-5937-44cb-857a-d3829301c73b -Url $HubUrl
}

In the code snippet above, we verify that content type publishing is enabled on the target site collection. If it is not enabled, we enable the feature to support content type publishing. Let's proceed with publishing a content type.

$Publisher = New-Object Microsoft.SharePoint.Taxonomy.ContentTypeSync.ContentTypePublisher($HubSite)
$ContentTypes = "Policy Document", "Contract Document"

$ContentTypes | ForEach-Object `
{
    $ContentType = $HubSite.RootWeb.ContentTypes[$_]
    $Publisher.Publish($ContentType)
}

$HubSite.Dispose()

In the code snippet above, I have created a simple array of content type names. Since content types must be published individually, this array allows me to loop through and publish one or more content types easily.

 Publishing a content type requires creating a new ContentTypePublisher object, and calling the Publish() method. If a content type has already been published, calling the publish method will republish the content type automatically.

Next Steps


Last Update:


next webcast button


next tip button



About the author
MSSQLTips author Chris Beckett Chris Beckett is a Business Solutions Architect, Mentor and Trainer with 20 years of experience.

View all my tips





Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name    *Email    Email me updates 


Signup for our newsletter
 I agree by submitting my data to receive communications, account updates and/or special offers about SQL Server from MSSQLTips and/or its Sponsors. I have read the privacy statement and understand I may unsubscribe at any time.



    



Learn more about SQL Server tools