kwizcom banner advertisement

SharePoint Online: Delete a List Item using PowerShell

Requirement: SharePoint Online Delete List Item
delete list item in sharepoint online using powershell

SharePoint Online PowerShell to Delete List Item
Here is my PowerShell script to delete an item from SharePoint online list.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Delete-ListItem()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $ItemID
    )

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get the List and item
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $ListItem = $List.GetItemById($ItemID)
        $ListItem.DeleteObject()
        $Ctx.ExecuteQuery()

        Write-Host "List Item Deleted successfully!" -ForegroundColor Green

    }
    Catch {
        write-host -f Red "Error Deleting List Item!" $_.Exception.Message
    } 
}

#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$ItemID="1"

#Call the function to rename column
Delete-ListItem -SiteURL $SiteURL -ListName $ListName -ItemID $ItemID

SharePoint Online: Bulk Delete List Items Matching Given Condition:
Say, we want to delete all inactive projects from "Projects" list.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Delete-ListItems()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName,
        [Parameter(Mandatory=$true)] [string] $CAMLQuery
    )

    Try {
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Define Query to get List Items
        $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
        $Query.ViewXml =$CAMLQuery
        
        #Get the List
        $List = $Ctx.Web.Lists.GetByTitle($ListName)

        #Get All List Items matching the query
        $ListItems = $List.GetItems($Query)
        $Ctx.Load($ListItems)
        $Ctx.ExecuteQuery()

        Write-host "Total Number of Items Found to delete: $($ListItems.count)"
        if($ListItems.count -gt 0)
        {
            #Loop through each item and delete
            For ($i = $ListItems.Count-1; $i -ge 0; $i--)
            {
                $ListItems[$i].DeleteObject()
            } 
            $Ctx.ExecuteQuery()

            Write-Host "List Items Deleted successfully!" -ForegroundColor Green
        }
    }
    Catch {
        write-host -f Red "Error Deleting List Items!" $_.Exception.Message
    } 
}

#Parameters
$SiteURL="https://crescent.sharepoint.com"
$ListName="Projects"
$CAMLQuery="<View><Query><Where><Eq><FieldRef Name='IsActive' /><Value Type='Boolean'>0</Value></Eq></Where></Query></View>"

#Call the function to rename column
Delete-ListItems -SiteURL $SiteURL -ListName $ListName -CAMLQuery $CAMLQuery 

Related Post: SharePoint Online Bulk Delete All List Items using PowerShell
SharePoint Online: Delete a List Item using PowerShell SharePoint Online: Delete a List Item using PowerShell Reviewed by Salaudeen Rajack on January 03, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.