Friday, 19 November 2010

Republish all hub content types in SharePoint using PowerShell

The idea for this script came in as a request from an anonymous comment I received yesterday on an earlier post showing how to push changes from a content type hub site to site collections using PowerShell.

The script in this article steps through each content type in a content type hub and republishes any that have been previously published. Once you specify the content type hub URL, the script performs the following tasks:

  • Checks to see if the URL specified is a content type hub site
  • Enumerates through each site content type in the hub and:
    • Checks if the content type has been previously published
    • If it has, republish it and write a success message to the console
    • If it hasn’t, write a message to the console confirming it is not a published content type

To use it, run this script first:

function Republish-HubContentTypes ($CTHubURL)
{
    #Get Content Type site and web objects
    $ctHubSite = Get-SPSite $CTHubURL
    $ctHubWeb = $ctHubSite.RootWeb

    #Check the site is a content type hub
    if ([Microsoft.SharePoint.Taxonomy.ContentTypeSync.ContentTypePublisher]::IsContentTypeSharingEnabled($ctHubSite))
    {
        #Set up ContentTypePublisher object to allow publishing through the Content Type Hub site
        $spCTPublish = New-Object Microsoft.SharePoint.Taxonomy.ContentTypeSync.ContentTypePublisher ($ctHubSite)
       
        #Step through each content type in the content type hub
        $ctHubWeb.ContentTypes | Sort-Object Name | ForEach-Object {
           
            #Has the content type been published?
            if ($spCTPublish.IsPublished($_))
            {
                #Republish content type
                $spCTPublish.Publish($_)
                write-host "Content type" $_.Name "has been republished" -foregroundcolor Green
            }
            else
            {
                write-host "Content type" $_.Name "is not a published content type"
            }
        }
    }
    else
    {
        write-host $CTHubURL "is not a content type hub site"
    }
    #Dispose of site and web objects
    $ctHubWeb.Dispose()
    $ctHubSite.Dispose()
}

Once run, you can call the script by typing the following command:

Republish-HubContentTypes –CTHubURL <ContentTypeHubURL>

For example, type the following command to republish all content types in a hub site with the URL http://portal/sites/CTHub:

Republish-HubContentTypes -CTHubURL http://portal/sites/CTHub

An example output from the script is shown below:

image

22 comments:

  1. this is perfect for what i needed to do today, thank you for posting it.

    ReplyDelete
  2. It's a pleasure Jeff - thanks for saying "thanks" :)

    ReplyDelete
  3. Hi Phil,

    I am trying to excute the above the script but iam getting below .

    "Exception calling "Publish" with "1" argument(s): "The operation failed on one or more Metadata web service application proxies: Metadata Service Application"

    Any help on the above issue would be higly appriciated.

    ReplyDelete
  4. I also faced the samie issue, open the powershell with runas administrator.

    ReplyDelete
  5. Great example, thank you.

    ReplyDelete
  6. You are a hero - thanx

    ReplyDelete
  7. I was just about to write this. You saved me a lot of time. Thanks loads.

    I like the check to see if it has been published before too.

    http://www.sharepointshowboat.com

    ReplyDelete
  8. Also had the "Exception calling "Publish" with "1" argument(s): "The operation failed on one or more Metadata web service application proxies: Metadata Service Application" Error.

    And RunAs Administrator did not solve it.

    Any ideas anyone...

    ReplyDelete
    Replies
    1. For those of you getting the error:
      "Exception calling "Publish" with "1" argument(s): "The operation failed on one or more Metadata web service application proxies: Metadata Service Application"

      And the ULS report errors for powershell.exe such as:
      Metadata web service application proxy Managed Metadata Service Proxy is not in a working state. Skip it.

      I had this error - and solved it by running the script on a server where "Managed Metadata Web Service" was started
      CA -> System -> Manage services on server (if this is not visible, launch IE as admin)

      Delete
  9. Just what I needed as well, only I had a very "funny" (frustrating) issue. The published content types would get published to the subscribing site, only without their names.. Have to manually go in and press publish through the UI.

    ReplyDelete
  10. I wrote a similar function to yours: I get my content types by filtering on the group names . One thing I found is for each new Content type you need to do an initial publish say with the UI . Otherwise you get your else clause.-

    ReplyDelete
  11. Thanks For Your valuable posting, it was very informative. Am working in Erp Software Company In India

    ReplyDelete