Remove Content Types From Libraries

RemoveContentTypesFromLibraries.ps1


# RemoveContentTypesFromLibraries: Remove all Content Types listed in the input XML
# file from all document libraries in the given Site Collection.
# This assumes all documents have already been changed to Content Type Document.
# This ensures all doc libraries have type Document as the default type, and do
# not include the COB Content Types.
# Adapted from a script provided by MS Support 3/1/2013.
# Adapted by M. Cox 3/16/2013.
# Syntax example:
# .\RemoveContentTypesFromLibraries.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 "RemoveContentTypesFromLibraries: Processing Site Colleciton: " $siteUrl -BackgroundColor Yellow -ForegroundColor Black
$siteScope = Start-SPAssignment
$site = get-spsite $siteCollectionURL
# get the content types XML from the input XML file
$XmlFileInput = [xml](Get-Content($xmlFilePath))
$contentypeToRemove = 'OldContentType'
$contentTypeToAdd = $site.rootweb.ContentTypes["Document"]
foreach ( $web in $site.allwebs)
{
    Write-host "  Processing subsite " $web.url -ForegroundColor DarkBlue
    $webScope = Start-SPAssignment
   
    # 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 DarkGreen
        Foreach ( $list in $web.lists )
        {
            $LibTitle = $list.Title
           
            if ($list.BaseType -ne 'DocumentLibrary') {continue}
       
            write-host "      Processing library " $LibTitle -ForegroundColor Gray
            $needToSetListContentTypeOrder = $true
                   
            if ($web.AvailableContentTypes[$contentTypeToRemove] -eq $null ){continue} # web does not use Content Type
               
            if ($list.ContentTypes[$contentTypeToRemove] -eq $null ){continue} # list does not use Content Type
       
            write-host "      Found Content Type '" $contentTypeToRemove "' in library '" $list.Title "'" -ForegroundColor Gray
            try
            {           
                $contenttype = $list.ContentTypes[$contentTypeToRemove]
                if ($contenttype.Readonly -eq $true)
                {
                    $contenttype.ReadOnly = $false
                    $contenttype.Update()
                }
                $list.ContentTypes.Delete($contenttype.id)
                $list.Update()
                write-host "      Removed Content Type '" $contentTypeToRemove "' from library '" $list.title "' in site '" $web.url "'"  -BackgroundColor Yellow -ForegroundColor Black
                try
                {              
                    if($needToSetListContentTypeOrder)
                    {
                        if ($list.ContentTypes[$contentTypeToAdd.Name] -eq $null )
                        {
                            $list.ContentTypes.Add($contentTypeToAdd) | Out-Null
                            $Genlist = New-Object System.Collections.Generic.List[Microsoft.SharePoint.SPContentType]
                            $genlist.Add($list.ContentTypes[$contentTypeToAdd.Name])
                            $list.RootFolder.ContentTypeOrder | % { $genlist.add($_) }
                            $list.RootFolder.UniqueContentTypeOrder = $genlist
                            $list.rootfolder.Update()                
                        }
                        $needToSetListContentTypeOrder = $false
                    }
                }
                Catch
                {
                    Write-host "Exception :" $_.exception.message -ForegroundColor red
                    Write-host "Web       :" $Web.url -ForegroundColor red
                    Write-host "List      :" $list.title -ForegroundColor red
                    Write-host "Cont Type :" $contentTypeToRemove -ForegroundColor red
                }
                Finally
                {
                }           
            }           
            Catch
            {
                Write-host "Web       :" $Web.url -ForegroundColor yellow
                Write-host "List      :" $list.title -ForegroundColor yellow
                Write-host "Exception :" $_.exception.message -ForegroundColor red
                       
            }
        }
    }
    Stop-SPAssignment $webScope

$site.dispose
Stop-SPAssignment $siteScope

No comments:

Post a Comment