Remove Content Types From Docs

RemoveContentTypesFromDocs.ps1

# Find all Documents that use a given Content Type and change those documents
# to use the base SharePoint Document content type instead.
# M. Cox 3/17/2013
# Takes, as input, the URL of a Site Collection and the path to an XML file that contains all the Content Types to Remove.
# Syntax example:
# .\RemoveContentTypesFromDocs.ps1
http://bmphub.int "D:\Scripts\ContentTypes.xml"
param(
    [string] $siteUrl = $(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 "RemoveContentTypesFromDocs: Processing Site Colleciton: " $siteUrl -BackgroundColor Yellow -ForegroundColor Black
$siteScope = Start-SPAssignment
$site = get-spsite $siteURL

# get the content types XML from the input XML file
$XmlFileInput = [xml](Get-Content($xmlFilePath))

# This sets the content type of documents back to the base SharePoint 'Document' content type
$DocumentContentTypeName = "Document"

foreach ( $web in $site.allwebs)
{
    Write-host "  Processing subsite " $web.url -ForegroundColor DarkBlue

    $webScope = Start-SPAssignment
   
    $docCT = $web.ContentTypes[$DocumentContentTypeName]

    foreach ( $list in $web.lists )
    {   
        if ($list.BaseType -ne 'DocumentLibrary') {continue}
       
        write-host "    Processing list " $list.Title -ForegroundColor DarkGreen
        $needToSetListContentTypeOrder = $true       
   
        # 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
           
            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 " $contentTypeToRemove " in list " $list.Title -ForegroundColor Gray
                       
            if($needToSetListContentTypeOrder)
            {  
                $docCT = $web.ContentTypes[$DocumentContentTypeName]
           
                # if the doc library does not have Content Type "Document", add it
                if ($list.ContentTypes[$DocumentContentTypeName] -eq $null )
                {
                    $docCT = $web.ContentTypes[$DocumentContentTypeName]
               
                    $list.ContentTypes.Add($docCT) | out-null
                }
                try
                {
                    $genlist = New-Object System.Collections.Generic.List[Microsoft.SharePoint.SPContentType]
                    $genlist.Add($list.ContentTypes[$DocumentContentTypeName])
                    $list.RootFolder.ContentTypeOrder | % { $genlist.add($_) }
                    $list.RootFolder.UniqueContentTypeOrder = $genlist
                    $list.Rootfolder.Update()                
                }
                Catch
                {
                    Write-host "Exception setting ContentTypeOrder:" $_.exception.message -ForegroundColor Red                           
                    Write-host "Web       :" $Web.url -ForegroundColor Red
                    Write-host "List      :" $list.title -ForegroundColor Red
                }
                $needToSetListContentTypeOrder = $false
            }
           
            $list.items | ForEach-Object {
                if ($_.ContentType.Name -eq $contentTypeToRemove)  # does item have the content?
                {
                    try
                    {
                        # if the item is a document make sure it's not checked out
                        if ($list.ForceCheckOut)
                        {
                            if ($_.File.CheckOutType -ne "None")
                            {
                                $_.File.UndoCheckOut()
                            }
                                   
                            # Change the content type associated with the document
                            $_.File.CheckOut()
                        }
                       
                        write-host "        Changing content type from '" $contenttypeToRemove "' to '" $DocumentContentTypeName "' in file '" $_.Url "'" -BackgroundColor Yellow -ForegroundColor Black
                       
                        if ($docCT -eq $null)
                        {
                            $docCT = $web.ContentTypes[$DocumentContentTypeName]
                        }
                        $_["ContentTypeId"] = $docCT.ID
                       
                        $_.SystemUpdate( $false )
                       
                        if ($list.ForceCheckOut)
                        {
                            $_.File.CheckIn("System update: Content type changed to " + $DocumentContentTypeName, 1)
                        }
                    }
                    Catch
                    {
                        Write-host "Web       :" $Web.url -ForegroundColor Red
                        Write-host "List      :" $list.title -ForegroundColor Red
                        Write-host "Exception :" $_.exception.message -ForegroundColor Red                           
                    }
                }
            }   
        } # foreach $contenttypeInXmlFile
    } # foreach $list
    $web.Update()
    Stop-SPAssignment $webScope
} # foreach $web 

$site.dispose
Stop-SPAssignment $siteScope

No comments:

Post a Comment