Office 365 – PowerShell Script to Create a List, Add Fields and Change the Default View all using CSOM

I'm my continued quest to get to grips with the Client Side Object Model (CSOM) in SharePoint 2013, I have put together a sample script below that connects to a Site Collection within an O365 tenant and does the following:

  • Creates a list using the "Custom" list template
  • Adds two Site Columns to the list (City and Company)
  • Adds these fields to the default view
  • Adds an item to the list

You may find this useful as a reference! The usual disclaimers apply 🙂

All you need to run this script is an O365 tenant, the SharePoint client components SDK installed on the machine running the script - and to update the $User, $SiteURL and $ListTitle variables. When the script is executed it will prompt for the password of the user specific in the $User variable.

#Specify tenant admin and site URL
$User = ""
$SiteURL =
$ListTitle = "List Title"

#Add references to SharePoint client assemblies and authenticate to Office 365 site - required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
$Password = Read-Host -Prompt "Please enter your password" -AsSecureString
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds

#Retrieve lists
$Lists = $Context.Web.Lists

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

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

#Grab city and company fields
$City = $Context.Web.AvailableFields | Where {$_.Title -eq "City"}
$Company = $Context.Web.AvailableFields | Where {$_.Title -eq "Company"}

#Add fields to the list

#Add fields to the default view
$DefaultView = $List.DefaultView

#Adds an item to the list
$ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$Item = $List.AddItem($ListItemInfo)
$Item["Title"] = "New Item"
$Item["Company"] = "Contoso"
$Item["WorkCity"] = "London"

Brendan Griffin

Comments (16)
  1. Maarten Ravestein says:

    Great example, thank you!

  2. yavnish says:

    great post….!!!


  3. Tony Ogilvie says:

    Hi – great post. Do you have any examples of creating lists from user-defined templates? Thanks!

  4. TheBarberFromPeru says:

    There are 2 errors in your code:

    $City = $SiteColumns = $Context.Web.AvailableFields | Where {$_.Title -eq "City"}
    should be
    $City = $Context.Web.AvailableFields | Where {$_.Title -eq "City"}

    (and the same for $Company)

  5. Brendan says:

    @TheBarberFromPeru – Well spotted will update!

  6. Sigurjón says:

    Hi, great post and thanks. I ran into problems with running it first since I assumed there where no errors with it 😉 I then realized I needed quotation marks around the site URL, then it ran fine. Thanks again.

  7. Anonymous says:

    I previously Blogged about creating a List and adding Site Columns –

  8. Shea says:

    So great! Been looking for days to find this exact solution. Thanks for taking the time to post this. You’ve been a lifesaver!

  9. Barry Noble says:

    This was very helpful. Thank you! I am trying to create a custom list from a list template I saved as a template into the List Template Library. Any thoughts on how to reference the template in the List Template Library. I have scoured the net for this
    and so far no luck. Regards, ~B

  10. Paul Perry says:

    @Barry: If you’re ok with getting a context object, then you can try something like this:

    function Get-CustomListTemplate{
    $templates = $Context.Site.GetCustomListTemplates($Context.Web)
    $templates | select baseType, Name, Description


    # Call the above function like this:

    Get-CustomListTemplate $Context "My List Template"


    Get-CustomListTemplate -Context $Context -listTemplateName "My List Template"

    I’ve just tested this against my online dev tenant, so hopefully it’ll work for yours!


  11. yashodip patil says:

    thanks ….its working

  12. Thangu says:

    Thank You very much!

  13. Christine says:

    It would be extra super great if this included a couple other column types like “choice” or “person”!

  14. Thangeswari says:


Comments are closed.

Skip to main content