Ensure_ContentType_Column_Order

Language:
PowerShell
Keywords:
SharePoint
Code Snippet

Function Ensure_ContentType_Column_Order( [Microsoft.SharePoint.SPContentType]$tContentType
                                        , [Microsoft.SharePoint.SPField]$tField
                                        , [System.Int16]$tOrder
                                        ) {
    if ($tContentType.FieldLinks[$tOrder - 1].Name -ne $tField.InternalName) {
        # use a List<String> to store the reordered fields
        $fieldList = New-Object 'System.Collections.Generic.List[System.String]'
        
        # add everything to the list EXCEPT the field of interest
        for($i = 1; $i -le $tContentType.FieldLinks.Count; $i++) {
            if ($tContentType.FieldLinks[$i - 1].Name -ne $tField.InternalName) {
                $fieldList.Add($tContentType.FieldLinks[$i - 1].Name)
            }
        }
        
        # now, using List<String>, inject the field of interest into the desired location
        $fieldList.Insert($tOrder - 1, $tField.InternalName)

        Write-Output([System.String]::Format("Content Type [{0}] : Column [{1}] : Moving to location [{2}]", $tContentType.Name, $tField.Title, $tOrder))
        $tContentType.FieldLinks.Reorder($fieldList.ToArray())
        $tContentType.Update()
    }
} # Function Ensure_ContentType_Column_Order(...)


Created 2011-10-13
comments powered by Disqus
Login