Wednesday, 14 September 2016

PowerShell script to create a list and add a lookup column

Many times we got requirement for client or some to create a custom list and add look up column to it.

As we all know, we can do it thru SharePoint GUI.

But this post will help you to do via Powershell.

So lets get started :)

Adding a List

$SpWeb = Get-SPWeb -Identity "http://SharePoint"
$SpTemplate = $SpWeb.ListTemplates["Name of list"]
$SpListCollection = $SpWeb.Lists
$SpListCollection.Add("listName", "listName", $SpTemplate)

Adding Lookup Field

$ParentList = $SpWeb.Lists.item("ParentList")
$SpList = $WebObj.Lists["listName"]
$SpChildListLookupField = $SpList.Fields["ChildLookupField"]
$SpChildListLookupField.LookupField = $ParentList.Fields["ParentLookupField"]

Happy SharePointing :)

No comments:

Post a Comment