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 - https://blogs.technet.com/b/fromthefield/archive/2014/02/18/office365-script-to-create-a-list-add-fields-and-change-the-default-view-using-csom.aspx

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 Elementshttps://msdn.microsoft.com/en-us/library/office/ms437580.aspx - 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 = "admin@tenant.onmicrosoft.com"
$Site = https://tenant.sharepoint.com/sites/sitename
$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
$List.Update()
$Context.ExecuteQuery()

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

#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'>
                            <CHOICES>
                                <CHOICE>Office 365</CHOICE>
                                <CHOICE>General</CHOICE>
                                <CHOICE>Email</CHOICE>
                                <CHOICE>OneDrive</CHOICE>
                                <CHOICE>SharePoint</CHOICE>
                                <CHOICE>Office Apps</CHOICE>
                                <CHOICE>Office Online</CHOICE>
                                <CHOICE>Other</CHOICE>
                            </CHOICES></Field>",$true,[Microsoft.SharePoint.Client.AddFieldOptions]::AddFieldToDefaultView)
$List.Update()
$Context.ExecuteQuery()

Voilà the list has been created!

Brendan Griffin - @brendankarl