1

Create document library with look up and multi-line text box fields using the below PowerShell

Remove-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

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

#List type or template 

$spTemplate = [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary 

#Get all the lists to the listcollection
$spListCollection=$spWeb.Lists

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

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

#get the list to the list object
$spList = $spWeb.Lists["Student1"]

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

$ParentList = $spWeb.Lists.item("Studentlist")
$spList = $spWeb.Lists["Student1"]
$spList.Fields.AddLookup("lookupcol",$ParentList.id,$true)
$spChildListLookupField = $spList.Fields["lookupcol"]
$spChildListLookupField.LookupField = $ParentList.Fields["ID"]
$spChildListLookupField.Indexed = $true
$spChildListLookupField.RelationshipDeleteBehavior = [Microsoft.SharePoint.SPRelationshipDeleteBehavior]::Restrict
$spChildListLookupField.Update()


$Views = $spList.Views["All Items"]
$Views.ViewFields.Add("COMMENTS")
$Views.ViewFields.Add("lookupcol")


$Views.Update()

$spWeb.Dispose()

I am getting errors -

You cannot call a method on a null-valued expression - $spList.Fields.Add("COMMENTS",$spFieldType,$false)

  • Make sure the list got created before adding fields. – Amal Hashim Mar 30 '15 at 19:21
  • I added $spList = $spWeb.Lists["Student1"] $spList.update() - so now the list is getting created and columns are added. but i am getting error at viewfeilds - You cannot call a method on a null-valued expression. + $Views.ViewFields.Add("COMMENTS") – mak Mar 30 '15 at 19:39
  • Ok now the view is fine. I changed from $Views = $spList.Views["All Items"] to $Views = $spList.Views["All Documents"] - Thanks ! – mak Mar 30 '15 at 19:47

Your Answer

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

Browse other questions tagged or ask your own question.