共用方式為


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 - https://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

  • Anonymous
    January 01, 2003
    thank you
  • Anonymous
    June 18, 2014
    Great example, thank you!
  • Anonymous
    July 16, 2014
    great post....!!!

    Thanks....
  • Anonymous
    October 29, 2014
    Hi - great post. Do you have any examples of creating lists from user-defined templates? Thanks!
  • Anonymous
    February 02, 2015
    Thanks
  • Anonymous
    February 12, 2015
    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)
  • Anonymous
    February 17, 2015
    @TheBarberFromPeru - Well spotted will update!
  • Anonymous
    February 25, 2015
    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.
  • Anonymous
    March 03, 2015
    I previously Blogged about creating a List and adding Site Columns - http://blogs.technet.com/b/fromthefield
  • Anonymous
    September 09, 2015
    So great! Been looking for days to find this exact solution. Thanks for taking the time to post this. You've been a lifesaver!
  • Anonymous
    January 19, 2016
    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
  • Anonymous
    February 10, 2016
    @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
  • Anonymous
    March 29, 2016
    thanks ....its working
  • Anonymous
    April 17, 2016
    Thank You very much!
  • Anonymous
    May 31, 2016
    It would be extra super great if this included a couple other column types like "choice" or "person"!
  • Anonymous
    September 07, 2017
    Thanks