Last active
December 11, 2016 10:00
-
-
Save IAMPetro/84a10cd8304a5ff98b80d23cfb38cc3e to your computer and use it in GitHub Desktop.
SharePoint: Add Site Content Type
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 Add-SPSiteContentType | |
{ | |
<# | |
.Synopsis | |
This advanced function adds a Content Type to a Document Library based on search criteria. | |
.Description | |
This function will add a new Content Type into all libraries that contain the -lookForCT specified in the function. | |
This function also produces a series of reports capturing the work carried out in the function, and any errors experienced. | |
.Example | |
C:\> Add-SPSiteContentType –SiteUrl “http://yourdomain.com/sites/Finance -lookForCT "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 it will add the new Content Type 'Scanned Document'. | |
.Example | |
C:\> Add-SPSiteContentType –SiteUrl “http://yourdomain.com/sites/Finance -lookForCT "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: Add-SPSiteContentType | |
Author: Petro Margaritis | |
Last Edit: 19/05/2012 | |
Keywords: Content Types | |
.Link | |
http://www.iampetro.com/ | |
#> | |
Param ( | |
[parameter(Mandatory=$true)][string]$SiteUrl, | |
[parameter(Mandatory=$true)][string]$lookForCT, | |
[parameter(Mandatory=$true)][string]$NewCType, | |
[parameter(Mandatory=$true)][string]$outputPath, | |
[string]$SmtpServer, | |
[string]$EmailFrom, | |
[string]$EmailTo | |
) | |
$site = Get-SPSite $SiteUrl | |
# Report variables | |
$ctypeNotExist = "NotExist" | |
$ctypeExist = "Exist" | |
$ctAdded = "True" | |
$ctNotAdded = "False" | |
# Output file name and path constructor | |
$SiteTitle = $site.RootWeb.Title | |
$fileName = "_Add_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 Add-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 specified at the start of the script | |
if (($_.ContentTypes | where { $_.Name -eq $lookForCT }) -eq $null) | |
{ | |
$outstring = $outWebTitle, $outWebURL, $outLibRootFolder, | |
$outLibTitle, $lookForCT, $NewCType, $ctypeNotExist, $ctNotAdded | |
$outstring -join "," >> $filePath | |
} | |
else | |
{ | |
# Add site content types to the list | |
$ParentContentType = $site.RootWeb.AvailableContentTypes[$NewCType] | |
$ctToAdd = New-Object Microsoft.SharePoint.SPContentType -ArgumentList @($ParentContentType, $site.RootWeb.ContentTypes, $NewCType) | |
$ct = $_.ContentTypes.Add($ctToAdd) | |
$_.Update() | |
# Resets the New Menu content drop down button to include all of the libraries content types | |
$_.rootfolder.UniqueContentTypeOrder = $null | |
$_.rootfolder.Update() | |
$outstring = $outWebTitle, $outWebURL, $outLibRootFolder, $outLibTitle, $lookForCT, $NewCType, $ctypeExist, $ctAdded | |
$outstring -join "," >> $filePath | |
write-host "Content type" $ct.Name "added to 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 = "_Add_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 Add-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