2

I need a Script to create a custom list and add a look up column into it and add the column into a default view. The look up column should have relationship of Restrict delete.

8

Adding List

$spWeb = Get-SPWeb -Identity "http://url"
$spTemplate = $spWeb.ListTemplates["Custom List"] 
$spListCollection = $spWeb.Lists 
$spListCollection.Add("listName", "listName", $spTemplate) 

Adding Lookup Field with Restrict Delete

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

Adding field to View

$spView = $spList.DefaultView
$spView.ViewFields.add($spChildListLookupField)
$spView.Update()
  • @mak do not forget to mark as answer if it solves your problem. (I think your answer is pretty good) – Nico Mar 31 '15 at 14:44
  • what is $WebObj? – Vignesh Subramanian Jan 30 '17 at 9:33

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.