0
  1. I am getting into a null array - $spList = $webobj.Lists["Studentlist1"]
  2. Exception calling "update" with "0" argument(s): "This lookup field cannot enforce a relationship behavior because it is not indexed.

I am getting above two errors when i run the below powershell script to create a lookup column in a list "Studentlist1" calling "ID" column from a parent list "Studentlist"

#To which site u want to create the list 
$spWeb=Get-SPWeb -Identity http://XYZ

#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("Studentlist1","Studentlist1",$spTemplate)

#get the path of subsite and sitecollecion 
$path = $spWeb.url.trim()

#get the list to the list object
$spList = $spWeb.GetList("$path/Lists/Studentlist1")

#adding the field type(Number) to the list
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Number
$spList.Fields.Add("SNo",$spFieldType,$false)

#adding the field type(Text) to the list
$spFieldType = [Microsoft.SharePoint.SPFieldType]::Text
$spList.Fields.Add("SName",$spFieldType,$false)

$ParentList = $spWeb.Lists.item("Studentlist")
$spList = $WebObj.Lists["Studentlist1"]
$spList.Fields.AddLookup("ChildLookupField",$ParentList.id,$false)
$spChildListLookupField = $spList.Fields["ChildLookupField"]
$spChildListLookupField.LookupField = $ParentList.Fields["ID"]
$spChildListLookupField.RelationshipDeleteBehavior = [Microsoft.SharePoint.SPRelationshipDeleteBehavior]::Restrict
$spChildListLookupField.Update()

$Views = $spList.Views["All Items"]
$Views.ViewFields.Add("SNo")
$Views.ViewFields.Add("SName")
$Views.ViewFields.Add("ChildLookupField")


$Views.Update()
1

For first issue

Replace

#adding the new list to the list collection
$spListCollection.Add("Studentlist1","Studentlist1",$spTemplate)

#get the path of subsite and sitecollecion 
$path = $spWeb.url.trim()

#get the list to the list object
$spList = $spWeb.GetList("$path/Lists/Studentlist1")

With

#adding the new list to the list collection
$lstId = $spListCollection.Add("Studentlist1","Studentlist1",$spTemplate)

#get the list to the list object
$spList = $spWeb.Lists[[System.GUID]($lstId)]

For second issue

Replace

$spList.Fields.AddLookup("ChildLookupField",$ParentList.id,$false)
$spChildListLookupField = $spList.Fields["ChildLookupField"]
$spChildListLookupField.LookupField = $ParentList.Fields["ID"]
$spChildListLookupField.RelationshipDeleteBehavior = [Microsoft.SharePoint.SPRelationshipDeleteBehavior]::Restrict
$spChildListLookupField.Update()

with

$spList.Fields.AddLookup("ChildLookupField",$ParentList.id,$false)
$spChildListLookupField = $spList.Fields["ChildLookupField"]
$spChildListLookupField.LookupField = $ParentList.Fields["ID"]
$spChildListLookupField.Indexed = $true
$spChildListLookupField.RelationshipDeleteBehavior = [Microsoft.SharePoint.SPRelationshipDeleteBehavior]::Restrict
$spChildListLookupField.Update()

The field has to be indexed before creating relationship delete behavior.

  • Thanks for your response. The second issue is resolved by adding indexed to true. but i am still having first error. Cannot index into a null array$spList = $WebObj.Lists["Studentlist1"]. You told me to check the list title is Studentlist1 right ?? I am creating this list using this code only right. how could it have different name. – mak Mar 30 '15 at 15:00
  • @mak please try updated answer – Arsalan Adam Khatri Mar 30 '15 at 15:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.