Ensure_List_Column

Language:
PowerShell
Keywords:
SharePoint
Code Snippet

## Ensures that an SPField exists within an SPList.. if it does, do nothing; if it does not, create it based on parameters
Function Ensure_List_Column( [Microsoft.SharePoint.SPList]$tList
                           , $tFieldName
                           , [Microsoft.SharePoint.SPFieldType]$SPFieldType
                           , $isRequired
                           ) {
    if (!$tList.Fields.ContainsField($tFieldName)) {
        Write-Output([System.String]::Format("List [{0}] : Adding Column [{1}]", $tList.Title, $tFieldName))
        $tList.Fields.Add($tFieldName, $SPFieldType, $isRequired) | Out-Null
        $tList.Update()
    }
} # Function Ensure_List_Column(...)


Created 2011-10-13
comments powered by Disqus
Login