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

InfoPath alternatives for form designing SharePoint

In this post we will discuss how we can delete list items in SharePoint online Office 365 using 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.

Also you can read my previous posts:
– How to assign permission level to SharePoint 2013 groups using PowerShell?

– Create SharePoint 2013 List using out of box or custom template using PowerShell

– Get Size of sub sites PowerShell Script SharePoint 2013 or 2007

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()

}

}

Hope this will help.

Similar SharePoint 2013 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 →

Leave a Reply