Skip to content

Instantly share code, notes, and snippets.

@aaira-a
Last active February 24, 2021 14:39
Show Gist options
  • Save aaira-a/eba51bfbf922d34e8c87 to your computer and use it in GitHub Desktop.
Save aaira-a/eba51bfbf922d34e8c87 to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link

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

@gfrice
Copy link

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