How to delete list items in SharePoint Online using Client Side Object Model with PowerShell?

Check out Best Alternative to InfoPath


In this post, we will discuss how we can delete list items in SharePoint online Office 365 using the Client Side Object Model (CSOM) with PowerShell. If you have more items to delete from SharePoint List, we can go for batch delete.


I would like to highlight programmatically when we delete list items, those are not moved to the Recycle Bin for the site.

Please find the below code.

cls

Add-Type -Path “E:\PSDLL\Microsoft.SharePoint.Client.dll”

Add-Type -Path “E:\ PSDLL\Microsoft.SharePoint.Client.Runtime.dll”

$0 = $MyInvocation.MyCommand.Definition

$dp0 = [System.IO.Path]::GetDirectoryName($0)

$url = “https://onlysharepoint2013.sharepoint.com/sites/test123/”

$username = “sambitha@microsofttest.com”

$password = “*****”

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force

#connect/authenticate to sharepoint online and get ClientContext object…

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)

$clientContext.Credentials = $credentials

if(!$clientContext.ServerObjectIsNull.Value)

{

$web = $clientContext.Site.RootWeb

$clientContext.Load($web)

$clientContext.ExecuteQuery()

$list=$clientContext.Web.Lists.GetByTitle(‘TestList’)

$clientContext.Load($list)

$clientContext.ExecuteQuery()

$query = New-Object Microsoft.SharePoint.Client.CamlQuery

$query.ViewXml=”<View><RowLimit>1000</RowLimit></View>”

$items=$list.GetItems($query)

$clientContext.Load($items)

$clientContext.ExecuteQuery()

if ($items.Count -gt 0)

{

for ($i = $items.Count-1; $i -ge 0; $i–)

{

$items[$i].DeleteObject()

}

$clientContext.ExecuteQuery()

}

}

Read some SharePoint csom articles:


Hope this will help.


You May Also like the Following SharePoint Online Tutorials:

About Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

View all posts by Sambita Rath →