Ensure_ContentType_Column

Language:
PowerShell
Keywords:
SharePoint
Code Snippet

## Ensures that a Site Column exists within a Content Type.. if it does, do nothing; if it does not, add it
Function Ensure_ContentType_Column( [Microsoft.SharePoint.SPContentType]$tContentType
                                  , [Microsoft.SharePoint.SPField]$tField
                                  ) {
    if (!$tContentType.Fields.Contains($tField.Id)) {
        $tFieldLink = New-Object Microsoft.SharePoint.SPFieldLink($tField)
        $tContentType.FieldLinks.Add($tFieldLink)

        Write-Output([System.String]::Format("Content Type [{0}] : Adding Column [{1}]", $tContentType.Name, $tField.Title))
        $tContentType.Update()
    }    
} # Function Ensure_ContentType_Column(...)


Created 2011-10-13
comments powered by Disqus
Login