Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

Add Lookup Field to SharePoint List using PowerShell

$
0
0
PowerShell script to add lookup field to SharePoint list programmatically:

Scenario: You have a parent list called "Parent Projects" and child list "Project Milestones". The "Parent Project Name" field from child list is being looked up from the parent list's "Project Name" field.
PowerShell Script to Add Lookup Field to SharePoint List:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

#configuration parameters
$WebURL="https://portal.crescent/Projects/"
$ParentListName="Parent Projects"
$ChildListName="Project Milestones"

$ParentLookupColumnName="Project Name"
$ChildLookupColumnName="Parent Project Name"

#Get the Parent and Child Lists
$Web = Get-SPWeb $WebURL
$ParentList = $web.Lists[$ParentListName]
$ChildList = $web.Lists[$ChildListName]

#Check if Field exists already
if(!$childList.Fields.ContainsField($ChildLookupColumnName))
{
#Add Lookup Field
$ChildLookupColumn = $ChildList.Fields.AddLookup($ChildLookupColumnName,$ParentList.id,$False)
$ChildLookupColumn = $ChildList.Fields[$ChildLookupColumnName]

#Setup lookup Field property
$ChildLookupColumn.LookupField = $ParentList.Fields[$ParentLookupColumnName]
#$ChildLookupColumn.AllowMultipleValues=$true
$ChildLookupColumn.update()
write-host "Lookup field added successfully!" -f green
}
else
{
write-host "Field Exists already!" -f red
}


Viewing all articles
Browse latest Browse all 1058

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>