Posts Tagged ‘PowerShell’

Add documentset welcomepage fields using powershell

10/19/2012

Recently I came across the problem of adding the contenttype of a created documentset to the welcomepage fields. This could not be done through documentset settings on the content type in the GUI. After a few minutes tinkering I got a powershell script to add the field I needed.

Here’s the powershell script I ended up using:


[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.DocumentManagement")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$weburl = "http://myweburl"
$web = Get-SPWeb $weburl
$lib = $web.Lists["Listname"]
$ctfield = $null
foreach($item in $lib.Items | Where-Object {$_.Folder -eq $null})
{
    Write-Host "Fields of " $item.Name
    foreach($field in $item.Fields)
    {
        if($field.Title -eq "Content Type")
        {
            Write-Host "Found field " $field.Title
            $ctfield = $field
        }
    }
    Write-Host ""
}
foreach($item in $lib.Items | Where-Object {$_.Folder -ne $null})
{
    Write-Host "Adding content type field to " $item.Name
    $ds =  [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::GetDocumentSet($item.Folder)
    $dst = $ds.ContentTypeTemplate
    $dst.WelcomePageFields.Add($ctfield)
    $dst.Update($true)
    Write-Host ""
}

Since I’m updating the content type template I assume it would be possible to just update the first documentset found in the collection, however I couldn’t get the break to work properly for these loops so I just ignored that fact for now.