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 – http://www.microsoft.com/en-us/download/details.aspx?id=35585 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 = "admin@tenant.onmicrosoft.com"
$SiteURL = https://tenant.sharepoint.com/sites/site
$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
$Context.Load($Lists)
$Context.ExecuteQuery()

#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
$List.Update()
$Context.ExecuteQuery()

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

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

#Add fields to the list
$List.Fields.Add($City)
$List.Fields.Add($Company)
$List.Update()
$Context.ExecuteQuery()

#Add fields to the default view
$DefaultView = $List.DefaultView
$DefaultView.ViewFields.Add("City")
$DefaultView.ViewFields.Add("Company")
$DefaultView.Update()
$Context.ExecuteQuery()

#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"
$Item.Update()
$Context.ExecuteQuery()


Brendan Griffin

Comments (15)

  1. Maarten Ravestein says:

    Great example, thank you!

  2. yavnish says:

    great post….!!!

    Thanks….

  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 –
    http://blogs.technet.com/b/fromthefield

  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{
    param([Microsoft.SharePoint.Client.ClientContext]$Context,
    [string]$listTemplateName
    )
    $templates = $Context.Site.GetCustomListTemplates($Context.Web)
    $Context.Load($templates)
    $Context.ExecuteQuery()
    $templates | select baseType, Name, Description

    }

    # Call the above function like this:

    Get-CustomListTemplate $Context "My List Template"

    #or:

    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!

    ~P

  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”!