Ensure_SiteColumn_OfType_Lookup_LookupField

Sets the Lookup Field for an existing lookup column
Language:
PowerShell
Keywords:
SharePoint
Code Snippet

Function Ensure_SiteColumn_OfType_Lookup_LookupField( [Microsoft.SharePoint.SPField]$tField
                                                    , [Microsoft.SharePoint.SPField]$tTargetField
                                                    ) {
    # Validate that we are using an SPFieldChoice
    if ($tField.GetType() -ne [Microsoft.SharePoint.SPFieldLookup]) {
        throw New-Object System.ArgumentException([System.String]::Format("Field [{0}] is not of type Lookup", $tField.Title))
    }

    if ($tField.LookupField -ne $tTargetField.InternalName) {
        $tField.LookupField = $tTargetField

        Write-Output([System.String]::Format("Column [{0}] : Setting Lookup Field [{1}]", $tField.Title, $tTargetField.Title))
        $tField.Update()
    }

} # Function Ensure_SiteColumn_OfType_Lookup_LookupField(...)


Created 2011-10-13
comments powered by Disqus
Login