Last active
December 11, 2016 10:06
-
-
Save IAMPetro/5dcb61646b4d8c913660fec1306d4677 to your computer and use it in GitHub Desktop.
SharePoint: Remove Site Content Types
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
function Remove-SPSiteContentType | |
{ | |
<# | |
.Synopsis | |
This advanced function iterates through all documents within a document library and changes the | |
identified Content Type to a new Content Type. | |
.Description | |
This function will change document Content Types for all items within a document library whose Content Type | |
is specified as -CTypeToRemove, and changes it to the new Content Type -lookForCT. | |
It will also override any items that are checked out to users as it iterates through the library. | |
This function also produces reports capturing the work carried out in the function, and any errors experienced. | |
.Example | |
C:\> Remove-SPSiteContentType –SiteUrl “http://yourdomain.com/sites/Finance –lookForCT “Document” –CTypeToRemove “Scanned Document” -outputPath "C:\Script_Reports\" | |
This example looks across the 'Finance' site collection for libraries that contain the Content Type 'Document'. | |
If the library also contains the 'Scanned Document' Content Type, it will delete it. | |
.Example | |
C:\> Remove-SPSiteContentType –SiteUrl “http://yourdomain.com/sites/Finance –lookForCT “Document” –CTypeToRemove “Scanned Document” -outputPath "C:\Script_Reports\" -SmtpServer "mail01.yourdomain.com" -EmailFrom "SPTask@yourdomain.com" -EmailTo "You@yourdomain.com" | |
This example is identical to Example 1, however it includes email notifications - This is particularly good for large environments | |
where this function may take a long time to iterate through all libraries within the specified site colleciton. | |
.Notes | |
Name: Remove-SPSiteContentType | |
Author: Petro Margaritis | |
Last Edit: 19/05/2012 | |
Keywords: Content Types, List Items | |
.Link | |
http://www.iampetro.com/ | |
#> | |
Param ( | |
[parameter(Mandatory=$true)][string]$SiteUrl, | |
[parameter(Mandatory=$true)][string]$lookForCT, | |
[parameter(Mandatory=$true)][string]$CTypeToRemove, | |
[parameter(Mandatory=$true)][string]$outputPath, | |
[string]$SmtpServer, | |
[string]$EmailFrom, | |
[string]$EmailTo | |
) | |
# Get site object | |
$site = Get-SPSite $SiteUrl | |
# Report variables | |
$ctypeNotExist = "Not Exist" | |
$ctypeExist = "Exist" | |
$lookForCTNotExist = "Remove CType Not Found" | |
$lookForCTExist = "Remove CType Found" | |
$ctypeRemoved = "Removed" | |
$ctypeNotRemoved = "Not Removed" | |
$ctReadOnly = "Read-only" | |
# Output file name and path constructor | |
$SiteTitle = $site.RootWeb.Title | |
$fileName = "_Remove_CType.csv" | |
$filePath = $outputPath, $SiteTitle, $fileName | |
$filePath -join "" | |
if ($SmtpServer -gt 1 -and $EmailFrom -gt 3 -and $EmailTo -gt 3) | |
{ | |
# Send begin Email notification | |
$date = Get-Date | |
[string]$startSubject = "Starting the Remove-SPSiteContentType in the site collection - ", $siteTitle | |
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $startSubject -Body $date -SmtpServer $SmtpServer | |
} | |
# Walk through each site in the site collection | |
$site | Get-SPWeb -Limit all | ForEach-Object { | |
$outWebTitle = $_.Title | |
$outWebURL = $_.URL | |
write-host "Checking site:"$_.Title | |
# Go through each document library in the site | |
$_.Lists | where { $_.BaseTemplate -eq "DocumentLibrary" } | ForEach-Object { | |
$outLibTitle = $_.Title | |
$outLibRootFolder = $_.RootFolder | |
# Check to see if the library contains the content type we are looking for intially | |
if (($_.ContentTypes | where { $_.Name -eq $lookForCT }) -eq $null) | |
{ | |
# write-host "Content type" $ct.Name "does not exist in the following list" $_.Title | |
$outstring = $outWebTitle, $outWebURL, $outLibRootFolder, $outLibTitle, | |
$lookForCT, $CTypeToRemove, $ctypeNotExist, "", $ctypeNotRemoved | |
$outstring -join "," >> $filePath | |
} | |
else | |
{ | |
# Check to see if the library contains the content type we wish to delete | |
if (($_.ContentTypes | where { $_.Name -eq $CTypeToRemove }) -eq $null) | |
{ | |
# write-host "The content type" $CTypeToRemove "does not exist on list" $_.Title | |
$outstring = $outWebTitle, $outWebURL, $outLibRootFolder, $outLibTitle, $lookForCT, | |
$CTypeToRemove, $ctypeNotExist, $lookForCTNotExist, $ctypeNotRemoved | |
$outstring -join "," >> $filePath | |
} | |
else | |
{ | |
$ctToRemove = $_.ContentTypes[$CTypeToRemove] | |
# Check if the content type is Read-Only | |
if ($ctToRemove.Readonly -eq $true) | |
{ | |
# Change Read-Only to FALSE | |
$ctToRemove.ReadOnly = $false | |
$ctToRemove.Update() | |
write-host "Changing Content from Read-Only to False" $ctToRemove.Name "from list" $_.Title -foregroundcolor Cyan | |
# Remove content types from list | |
$_.ContentTypes.Delete($ctToRemove.Id) | |
$_.Update() | |
write-host "Removing content type" $ctToRemove.Name "from list" $_.Title -foregroundcolor Green | |
$outstring = $outWebTitle, $outWebURL, $outLibRootFolder, $outLibTitle, $lookForCT, | |
$CTypeToRemove, $ctypeExist, $lookForCTExist, $ctypeRemoved, $ctReadOnly | |
$outstring -join "," >> $filePath | |
} | |
else | |
{ | |
# Remove content types from list | |
$outstring = $outWebTitle, $outWebURL, $outLibRootFolder, $outLibTitle, $lookForCT, | |
$CTypeToRemove, $ctypeExist, $lookForCTExist, $ctypeRemoved | |
$outstring -join "," >> $filePath | |
$_.ContentTypes.Delete($ctToRemove.Id) | |
$_.Update() | |
write-host "Removing content type" $ctToRemove.Name "from list" $_.Title -foregroundcolor Green | |
} | |
} | |
} | |
} | |
} | |
# Dispose of the site object | |
$site.Dispose() | |
# Export any possible errors captured in the $error varilable to the corresponding error file | |
$errorFileName = "_Remove_CType_Errors.csv" | |
$errorOutput = $outputPath, $SiteTitle, $errorFileName | |
$errorOutput -join "" | |
$Error >> $errorOutput | |
$Error.Clear() | |
if ($SmtpServer -gt 1 -and $EmailFrom -gt 3 -and $EmailTo -gt 3) | |
{ | |
# Send Ending Email notification | |
$date = Get-Date | |
[string]$endSubject = "Finishing the Remove-SPSiteContentType in the site collection - ", $siteTitle | |
Send-MailMessage -From $EmailFrom -To $EmailTo -Subject $endSubject -Body $date -SmtpServer $SmtpServer | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment