Ive searched for answers to this, and cant seem to pinpoint my problem here elsewhere.

Basically what Im doing is that I have a PowerShell script which that,

1) creates a Content Type (ContentTypeX)

2) creates fields and adds to ContentTypeX

3) finds an existing list, Pages

4) Associates ContentTypeX to Pages

5) Add a new LookUp Field

6) Add that LookUp Field to the Default view of Pages

So my problem is that, if I created the lookup field on the list from the front-end, added it to the list will associate it to all the Content Types associated with that list.

With my below Powershell code, it seems that when it adds the new LookUp field to the list, it only associates it with what seems is the first content type of the list.

Cant figure it out but wandering why Im not getting the 'default' result of it associating/linking with all the content types when being added to the list.

Any ideas?

Code belows



function UpdatePagesList($rootWeb, $listName)
    UpdateList $rootWeb $listName

    $list = $rootWeb.Lists[$listName] 
    AddContentTypeToList $rootWeb $list "ContentTypeX"

    AddLookupField $list "Look Up Field Name" "LookUpFieldName" $true $true $false $rootWeb   "LookUpFieldList" "Title" $false "None"

    AddFieldToDefaultView $list "Category"



function AddContentTypeToList($rootWeb, $list, $contentTypeName)
    if($list -ne $null)
        $contentType = $siteWeb.ContentTypes[$contentTypeName]
        $contentTypeExists = $list.ContentTypes[$contentTypeName]

        #If content type is not found on the list 
        if ($contentTypeExists -eq $null)
            $listContentType = $list.ContentTypes.Add($contentType)
            write-host -f green "Content type $contentTypeName added to list $list"

            #Remove the default content type Item from the list
            RemoveContentType $list "Item"


function AddLookupField($target, $displayName, $internalName, $required,   $indexed, $unique, $web, $lookupListName, $lookupFieldName, $multiValue, $relationshipDeleteBehavior)
    if (($target.Fields | where { $_.Title -eq $displayName }) -eq $null)
        #Get the source list ID
        $lookupListId = $web.Lists[$lookupListName].ID

        #Add the lookup field to the list
        $target.Fields.AddLookup($internalName, $lookupListId, $required)

        #Retrieve the field and update properties
        $lookupfield = $target.Fields[$internalName] -as [Microsoft.SharePoint.SPFieldLookup]
        $lookupfield.Indexed = $indexed
        $lookupfield.EnforceUniqueValues = $unique
        $lookupfield.ShowInEditForm = $true
        $lookupfield.ShowInNewForm = $true
        $lookupfield.Hidden = $false
        $lookupfield.AllowMultipleValues = $multiValue
        $lookupfield.LookupField = $lookupFieldName

        if($relationshipDeleteBehavior -eq "None")
            $lookupfield.RelationshipDeleteBehavior = [Microsoft.SharePoint.SPRelationshipDeleteBehavior]::None
        elseif ($relationshipDeleteBehavior -eq "Cascade")
            $lookupfield.RelationshipDeleteBehavior = [Microsoft.SharePoint.SPRelationshipDeleteBehavior]::Cascade
        elseif ($relationshipDeleteBehavior -eq "Restrict")
            $lookupfield.RelationshipDeleteBehavior = [Microsoft.SharePoint.SPRelationshipDeleteBehavior]::Restrict
            $lookupfield.RelationshipDeleteBehavior = [Microsoft.SharePoint.SPRelationshipDeleteBehavior]::None


        #Update the title to something better for display purposes
        $field = $target.Fields[$internalName]
        $field.Title = $displayName


function AddFieldToDefaultView($target, $fieldName)
    #Add the column to the default view
    $view = $target.DefaultView

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.