As i mentioned in my last post it’d be useful to be able to sort the content types in a document library’s new button. This builds on the examples in the previous post but it can be run on it’s own.
This script will attempt to set a default content type, if one is specified, but if it isn’t or the one listed can’t be found it’ll default to alphabetical.
Function Sort-ContentTypesInNewButton{ [CmdletBinding()] Param ( [parameter(Mandatory=$false)][string] $DefaultContentTypeName, [parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.SharePoint.SPList] $SPList) BEGIN { Write-Verbose "Begining Sort-ContentTypesInNewButton" } PROCESS { $rootFolder = $SPList.RootFolder #Get content types fromt the button $contentTypesInPlace = New-Object 'System.Collections.Generic.List[Microsoft.SharePoint.SPContentType]' $contentTypesInPlace = $rootFolder.UniqueContentTypeOrder #Has a default content type name been specified? if ($DefaultContentTypeName) { $contentType = $contentTypesInPlace | where { $_.Name -eq $DefaultContentTypeName } if ($contentType -ne $null) { #Add the default content type $sortedListOfContentTypes = New-Object 'System.Collections.Generic.List[Microsoft.SharePoint.SPContentType]' $sortedListOfContentTypes += $SPList.ContentTypes[$DefaultContentTypeName] #Remove the default content type from the list $contentTypesInPlace = $contentTypesInPlace | where {$_.Name -ne $DefaultContentTypeName} } else { Write-Error "$DefaultContentTypeName was not found in the list, sorting by Name alone" } } else { Write-Verbose "No default content type specified" } #sort the remaining content types and add the sorted list foreach ($contentType in $($contentTypesInPlace | Sort-Object -Property Name )) { #Add the content types $sortedListOfContentTypes = [Microsoft.SharePoint.SPContentType[]] $sortedListOfContentTypes + $contentType } $rootFolder.UniqueContentTypeOrder = [Microsoft.SharePoint.SPContentType[]] $sortedListOfContentTypes #Update the root folder $rootFolder.Update() Write-Verbose "ContentType(s) sorted and added to the new button in list $($SPList.Name)" } End{ Write-Verbose "Ending Sort-ContentTypesInNewButton" } }