Set default content type for a sharepoint list

20/12/2012 13:04

$siteUrl="http://server/sitecollection"
$defaultContentTypeName="Custom Content Type Name"
 
Start-SPAssignment -Global

$web = get-spweb $siteUrl
$list = $web.Lists["Pages"]
$folder = $list.RootFolder

$result = New-Object System.Collections.Generic.List[Microsoft.SharePoint.SPContentType]
$currentOrder = $list.ContentTypes

Write-Host "-------------- Original Content Type order ----------------"
Write-Host ""
$currentOrder|foreach{Write-Host $_.Name}

Write-Host "-----------------------------------------------------------"
#Find our content type and put it to beginning
foreach ($ct in $currentOrder)
{
 if ($ct.Name.Contains($defaultContentTypeName))
 {
   Write-Host "Setting '$defaultContentTypeName' content type as default for '$list' library..."
   $result.Add($ct)
   break;
 }
}

$folder.UniqueContentTypeOrder = $result
$folder.Update()

#Add also other content types (to be available in 'New Document' button)
foreach ($ct in $currentOrder)
{
 if (-not($ct.Name.Contains($defaultContentTypeName)))
 {
   $result.Add($ct)
 }
}

Write-Host ""
Write-Host "-------------- Final Content Type order ----------------"
Write-Host ""
$result|foreach{Write-Host $_.Name}

#Actually this item removal is strange, but it works, and crashes when I comment it out
$result = $result.RemoveAt(0)
$folder.UniqueContentTypeOrder = $result
$folder.Update()


Stop-SPAssignment -Global