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

27 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
  12. I am happy to see this post.Thank you for sharing the great information.ERP in chennai

    ReplyDelete
  13. 2016 was filled with more Air Jordan 12 releases than we could handle, and there’s actually still a couple Jordan 11 more dropping before the end of the year. What we didn’t see was any low-top versions of the Air Jordan 12 hit retailers.If you happen to be a fan Jordan Shoes 2016 of the 12 Low then the good news for you is that Jordan Brand plans on releasing a few different colorways in 2017 with one of them being previewed Jordan 7 Tinker Alternate for the first time up above. This Air Jordan 12 Low features a vibrant red suede all throughout the upper with black accents placed on the inner liner, laces, Jordan 13 Shoes mudguard, upper eyelets, heel tab, branding, and the back of the midsole. A red outsole wraps up the look.No exact release date is known yet for the Air Jordan Jordan Shoes 12 Low Red Suede but it is expected to release some time in 2017. Make sure you stay tuned to Kicks On Fire for updates.
    A woolen version of Jordans for sale the Air Jordan 11 is expected to be offered this year as part of JB’s standout Holiday 2016 lineup, which will join alongside the return of the iconic Air Cheap Jordans Jordan 11 Space Jam.As suggested, the basketball sneaker will host a wool constructed upper, which will essentially be similar to the upcoming AJ 3 Wool and AJ 12 Wool Kobe Bryant Shoes renditions as well. Dark grey and black comprises the model’s upper, while a white midsole unit and translucent outsole finishes off the more functional and premium profile.
    It Jordan 13 doesn’t get much more official than this. If you had any doubts that the Air Jordan 11 Space Jam would be returning this year then you can put those Jordan Eclipse doubts to rest as Nike has now given us our first official look at the highly anticipated release.Part of Nike’s “12 Soles” Collection that will be dropping in December, Kyrie Shoes the Air Jordan 11 Space Jam will come with an OG look as it features a higher patent leather cut in comparison to the last couple of releases.

    ReplyDelete