Skip to content

Instantly share code, notes, and snippets.

@IAMPetro
Last active December 11, 2016 10:13
Show Gist options
  • Save IAMPetro/b60c4af85efdb17ac2b032a7548e1dc7 to your computer and use it in GitHub Desktop.
Save IAMPetro/b60c4af85efdb17ac2b032a7548e1dc7 to your computer and use it in GitHub Desktop.
SharePoint: Change Multiple File Content Types
function Change-SPMultiFileContentType
{
<#
.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 -oldCT and changes it to the new Content Type -newCType .
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:\> Change-SPMultiFileContentType –SiteUrl “http://yourdomain.com/sites/Finance –oldCT "Document" –newCType "Scanned Document" -outputPath "C:\Script_Reports\"
This example looks across the 'Finance' site collection for libraries that contain the Content Type 'Document'.
If the Content Type 'Document' exists within a library, the function will iterate through all of the items within the library
and will change the Content Type of documents containing the 'Document' Content Type to 'Scanned Document'.
.Example
C:\> Change-SPMultiFileContentType –SiteUrl “http://yourdomain.com/sites/Finance –oldCT "Document" –newCType "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: Change-SPMultiFileContentType
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]$oldCT,
[parameter(Mandatory=$true)][string]$newCType ,
[parameter(Mandatory=$true)][string]$outputPath,
[string]$SmtpServer,
[string]$EmailFrom,
[string]$EmailTo
)
# Get web, list and content type objects
$site = Get-SPSite $SiteUrl
# Report variables
$ctypeChanged = "Changed"
$ctypeNotChanged = "Unchanged"
# Output file name and path constructor
$SiteTitle = $site.RootWeb.Title
$fileName = "_ChangeMultiFile_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 Change-SPMultiFileContentType 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 library in the site
foreach ($list in $_.Lists | where { $_.BaseTemplate -eq "DocumentLibrary" }){
# Go through each item in the list
$list.Items | ForEach-Object {
$outLibTitle = $_.Title
$outLibRootFolder = $_.RootFolder
# Check if the item content type currently equals the old content type specified
if ($_.ContentType.Name -eq $oldCT)
{
# Check the check out status of the file
if ($_.File.CheckOutType -eq "None")
{
# Change the content type association for the item
$newCT1 = $list.ContentTypes[$newCType]
$newCTID = $newCT1.ID
$item = $_.file
$item.CheckOut()
write-host "Resetting content type for file" $_.Name "from" $oldCT "to" $newCType -foregroundColor Green
$_["ContentTypeId"] = $newCTID
$_.Update()
$item.CheckIn("Content type changed to " + $newCT1.Name, 1)
# Output results to file
$outstring = $outWebTitle, $outWebURL, $outLibRootFolder, $outLibTitle, $oldCT,
$newCT1.Name, $item.Name, $ctypeChanged
$outstring -join "," >> $filePath
}
else
{
write-host "File" $_.Name "is checked out to" $_.File.CheckedOutByUser.ToString() "and cannot be modified"
$checkOutUser = $_.File.CheckedOutByUser.ToString()
# Override Checkout
$_.File.UndoCheckOut()
write-host "Checkout overriden" -foregroundcolor Yellow
# Change the content type association for the item
$newCT1 = $list.ContentTypes[$newCType]
$newCTID = $newCT1.ID
$item = $_.file
$item.CheckOut()
write-host "Resetting content type for file" $item.Name "from" $oldCT "to" $newCT1.Name -foregroundcolor Cyan
$_["ContentTypeId"] = $newCTID
$_.Update()
$item.CheckIn("Content type changed to " + $newCT1.Name, 1)
# Output results to file
write-host "File" $item.Name "has been overwritten and content type changed to" $newCT1.Name -foregroundcolor Yellow
$outstring = $outWebTitle, $outWebURL, $outLibRootFolder, $outLibTitle, $oldCT, $newCT1.Name,
$item.Name, $ctypeChanged, $checkOutUser
$outstring -join "," >> $filePath
}
}
else
{
# Output results to file
$outstring = $outWebTitle, $outWebURL, $list.RootFolder, $list.Title, $oldCT, $newCType, $_.File.Name, $ctypeNotChanged
$outstring -join "," >> $filePath
}
}
}
}
# Dispose of the site object
$site.Dispose()
# Export any possible errors captured in the $error varilable to the corresponding error file
$errorFileName = "_ChangeMultiFile_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 Change-SPMultiFileContentType 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