Created
February 13, 2015 15:40
-
-
Save rheid/ac43862993666947a008 to your computer and use it in GitHub Desktop.
SharePoint - Unpublish a published content type if the published content type no longer exists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-PSSnapIn Microsoft.SharePoint.PowerShell | |
if ($args.Count -ne 3) | |
{ | |
Write-Host "UnpublishContentTypes.ps1 <web url> <content type hub url> <unpublish>" | |
exit | |
} | |
$webUrl = $args[0] | |
$ctUrl = $args[1] | |
$unpublish = $args[2] | |
$web = Get-SPWeb $webUrl | |
$ctsite = Get-SPSite $ctUrl | |
$ctPublisher = New-Object Microsoft.SharePoint.Taxonomy.ContentTypeSync.ContentTypePublisher($ctsite) | |
foreach($ct in $web.ContentTypes) | |
{ | |
if ( $ct.XmlDocuments -like "*SharedContentType*") | |
{ | |
try | |
{ | |
if (-not $ctsite.RootWeb.ContentTypes[$ct.Name]) | |
{ | |
Write-Host "Found orphan Shared ContentType $($ct.Name) - ($($ct.ID))" | |
if($unpublish) | |
{ | |
$ctPublisher.UnPublish($ct) | |
Write-Host "$($ct.Name) - ($($ct.ID)) UnPublished successfully" | |
} | |
} | |
} | |
catch | |
{ | |
Write-Host "Error unpublishing ContentType $($ct.Name) - ($($ct.ID)): $error[0]" | |
} | |
} | |
} | |
if($unpublish) | |
{ | |
Start-SPTimerJob MetadataSubscriberTimerJob | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment