Instantly share code, notes, and snippets.

Embed
What would you like to do?
create a new list item on sharepoint online using powershell csom
# 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”
Add-Type -Path “C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll”
# Specify tenant admin and site URL
$SiteUrl = "mysiteurl"
$ListName = "mylistname"
$UserName = "myusername"
$SecurePassword = ConvertTo-SecureString "mypassword" -AsPlainText -Force
# Bind to site collection
$ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$ClientContext.Credentials = $credentials
$ClientContext.ExecuteQuery()
# Get List
$List = $ClientContext.Web.Lists.GetByTitle($ListName)
$ClientContext.Load($List)
$ClientContext.ExecuteQuery()
# Create Single List Item
$ListItemCreationInformation = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$NewListItem = $List.AddItem($ListItemCreationInformation)
$NewListItem["Title"] = 'xxx'
$NewListItem.Update()
$ClientContext.ExecuteQuery()
# Loop Create List Item
for ($i=11; $i -le 20; $i++)
{
$ListItemCreationInformation = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$NewListItem = $List.AddItem($ListItemCreationInformation)
$NewListItem["Title"] = "abc$($i)"
$NewListItem.Update()
$ClientContext.ExecuteQuery()
}
@jsmileyb

This comment has been minimized.

jsmileyb commented Feb 15, 2017

Thank you! This is very clean and got me exactly what I was aiming for.

I altered a bit for a project I'm working on that accommodates a mass, multi-column import using a .csv file. I'm more than happy to share if you like.

Cheers.

@potatoqualitee

This comment has been minimized.

potatoqualitee commented Jul 9, 2018

Fabulous snippet! Thank you. The DLLs are also available for download/redist at https://www.nuget.org/packages/Microsoft.SharePoint.Client.dll

@gfrice

This comment has been minimized.

gfrice commented Aug 7, 2018

@potatoqualitee Great addition. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment