Remove Content Types From Site Collection

RemoveContentTypesFromSiteCollection.ps1

# RemoveContentTypesFromSiteCollection: Remove all Content Types listed in the XML file from the
# Site Collection Content Type Gallery.
# This assumes all documents have already been changed to Content Type Document
# This assumes all doc libraries have had the content types removed, also.
# Syntax example:
# .\RemoveContentTypesFromSiteCollection.ps1 http://bmphub.int/departments "D:\Scripts\ContentTypes.xml"
#=========Parameter Section=============
param(
    [string] $siteCollectionURL = $(Throw "-- You must specify the URL of a site collection as parameter 1."), #required parameter
 [string] $xmlFilePath = $(Throw "-- You must specify the pathname of an XML file as parameter 2.") #required parameter
)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"
Write-host "RemoveContentTypesFromSiteCollection: Processing Site Colleciton: " $siteCollectionURL -BackgroundColor Yellow -ForegroundColor Black
$siteScope = Start-SPAssignment
$web = get-SPWeb $siteCollectionURL
# get the content types XML from the input XML file
$XmlFileInput = [xml](Get-Content($xmlFilePath))
   
# loop over content types listed in the input XML file
foreach( $ctElement in $XmlFileInput.ContentTypes.ContentType)
{
    $contentTypeToRemove = $ctElement.Name
       
    write-host "  Checking for Content Type " $contentTypeToRemove -ForegroundColor Gray
   
    $ct = $web.ContentTypes[$contentTypeToRemove]
    if ($ct)
    {
        write-host "    Found Content Type '" $contentTypeToRemove "' in site " $web -ForegroundColor Gray
        try
        {
            if ($ct.Readonly -eq $true)
            {
                $ct.ReadOnly = $false
                $ct.Update()
            }          
            $ct.Delete()
            $web.Update()  # may not be necessary
            write-host "    Deleted Content Type '" $contentTypeToRemove "' from site " $web -ForegroundColor Black
        }
        Catch
        {
            Write-host "Exception :" $_.exception.message -ForegroundColor red
            Write-host "Web       :" $Web.url -ForegroundColor red
            Write-host "Cont Type :" $contentTypeToRemove -ForegroundColor red
        }
    }

Write-host "RemoveContentTypesFromSiteCollection: Done"

No comments:

Post a Comment