Continution to Previous post , here we are going to see how to create lookup field from Power Shell in SharePoint 2013
Create a SharePoint Custom List Department with Lookup Column
SNAME - Lookup
SID -Number
#List type or template
$spTemplate = $spWeb.ListTemplates["Custom List"]
#Get all the lists to the listcollection
$spListCollection=$spWeb.Lists
#adding the new list to the list collection
$spListCollection.Add("Department","Department",$spTemplate)
#get the path of subsite and sitecollecion
$path = $spWeb.url.trim()
#get the list to the list object
$spList = $spWeb.GetList("$path/Lists/Department")
#adding the field type(Lookup) to the list
$LookupList= $spWeb.Lists["Student"]
$fieldXml='<Field Type="Lookup" DisplayName="SName" ShowField="SName" StaticName="SName" List="' + $LookupList.id + '" Name="SName"></Field>'
$spList.Fields.AddFieldAsXml($fieldXml,$true,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)
#adding the field type(Number) to the list
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Number
$spList.Fields.Add("SID",$spFieldType,$false)
$Views = $spList.Views["All Items"]
$Views.ViewFields.Add("SID")
$Views.Update()
Create a SharePoint Custom List Department with Lookup Column
SNAME - Lookup
SID -Number
#List type or template
$spTemplate = $spWeb.ListTemplates["Custom List"]
#Get all the lists to the listcollection
$spListCollection=$spWeb.Lists
#adding the new list to the list collection
$spListCollection.Add("Department","Department",$spTemplate)
#get the path of subsite and sitecollecion
$path = $spWeb.url.trim()
#get the list to the list object
$spList = $spWeb.GetList("$path/Lists/Department")
#adding the field type(Lookup) to the list
$LookupList= $spWeb.Lists["Student"]
$fieldXml='<Field Type="Lookup" DisplayName="SName" ShowField="SName" StaticName="SName" List="' + $LookupList.id + '" Name="SName"></Field>'
$spList.Fields.AddFieldAsXml($fieldXml,$true,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)
#adding the field type(Number) to the list
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Number
$spList.Fields.Add("SID",$spFieldType,$false)
$Views = $spList.Views["All Items"]
$Views.ViewFields.Add("SID")
$Views.Update()