Office 365 - Create a List and Add Custom Fields using CSOM

I previously Blogged about creating a List and adding Site Columns using CSOM and PowerShell -

I was recently asked to create a List and add some Custom Fields using PowerShell, in this specific case Site Columns weren't being used so I need to create the fields from scratch. The example below does the following:

  • Creates a list named "CustomList" - this can be changed by updating the $ListTitle variable
  • Adds 3 fields to the list
    • Question (Multiple Lines of Text)
    • Answer (Enhanded Rich Text)
    • QuestionType (Choice)

I found a really useful reference for Field Elements - I used this this to work out the XML required for each field type.

Simply update the highlighted variables and run the script - as I'm sure your requirements will be far different to this use the questions I have added as a reference point, the article above is really useful for working out what you need for each field type.

#Add references to SharePoint client assemblies and authenticate to Office 365 site
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Username = ""
$Site =
$ListTitle = "CustomList"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username,$Password)
$Context.Credentials = $Creds

#Create List
$ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$ListInfo.Title = $ListTitle
$ListInfo.TemplateType = "100"
$List = $Context.Web.Lists.Add($ListInfo)
$List.Description = $ListTitle

#Add Questions
#Question - Multiple Lines of Text
$List.Fields.AddFieldAsXml("<Field Type='Note' NumLines='6' DisplayName='Question'/>",$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)

#Answer - Enhanced Rich Text
$a = $List.Fields.AddFieldAsXml("<Field Type='Note' RichText='TRUE' RichTextMode='FullHtml' DisplayName='Answer'/>",$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$a = $List.Update()
$a = $Context.ExecuteQuery()

#Question Type - Choice
$a = $List.Fields.AddFieldAsXml("<Field Type='Choice' DisplayName='QuestionType'>
                                <CHOICE>Office 365</CHOICE>
                                <CHOICE>Office Apps</CHOICE>
                                <CHOICE>Office Online</CHOICE>

Voilà the list has been created!

Brendan Griffin - @brendankarl