1

I have written some code which adds a list client side in powershell :

   $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)

   $ctx.Credentials = $credentials

   $web = $ctx.Web 

   Invoke-LoadMethod -ClientObject $web

   $ctx.ExecuteQuery()

   $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
   $ListInfo.Title = $ListTitle
   $ListInfo.TemplateType = "100"
   $myList = $web.Lists.Add($ListInfo)
   $myList.Description = $ListTitle
   $myList.Update()
   $ctx.ExecuteQuery() 

What I want to do is create some fields to add to this. Tried this but it didn't work :

$myFieldPrivacyStatementurl = $myList.Fields.Add("PrivacyStatementUrls","URL",1)
$myList.Fields[$myFieldPrivacyStatementurl].Title = "Privacy Statement Urls"
$myList.Fields[$myFieldPrivacyStatementurl].Update()
$myFieldIsActive = $myList.Fields.Add("IsActive","Boolean",0)
$myList.Fields[$myFieldIsActive].Title = "Is Active"
$myList.Fields[$myFieldIsActive].Update()
$myFieldSortOrder = $myList.Fields.Add("SortOrder","Number",1)
$myList.Fields[$myFieldSortOrder].Title = "Sort Order"
$myList.Fields[$myFieldSortOrder].Update()
$ctx.ExecuteQuery()
3
  • Are you getting any error?
    – Aanchal
    Jun 25, 2014 at 9:03
  • YES, this works server side but I want client side
    – bimbob
    Jun 25, 2014 at 9:06
  • Get errors when do that piece of code client side
    – bimbob
    Jun 25, 2014 at 9:06

1 Answer 1

2

The error occurs since Add method does not accept 3 parameters.

How to add column into List via CSOM in PowerShell

The following methods are used for adding fields into List:

FieldCollection.Add method

Example: add an existing site column into List

#Retrieve List
$List = $Context.Web.Lists.GetByTitle($ListTitle)
$Context.Load($List)
$Context.ExecuteQuery()

#Retrieve site columns (fields)
$SiteColumns = $Context.Web.AvailableFields
$Context.Load($SiteColumns)
$Context.ExecuteQuery()

#Find an existing site column
$City =  $Context.Web.AvailableFields | Where {$_.Title -eq "City"}
$Context.Load($City)
$Context.ExecuteQuery()

#Add field to the List
$List.Fields.Add($City)
$List.Update()
$Context.ExecuteQuery()

FieldCollection.AddFieldAsXml method

Example: add a new Field into List

#Retrieve List
$List = $Context.Web.Lists.GetByTitle($ListTitle)
$Context.Load($List)
$Context.ExecuteQuery()

#Add new field to the list
$List.Fields.AddFieldAsXml("<Field Type='Geolocation' DisplayName='Location'/>",$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$List.Update()
$Context.ExecuteQuery()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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