CSOM SharePoint Online Delete List Using PowerShell
CSOM SharePoint Online Delete List Using PowerShell
Summary
In this Wiki let's explore PowerShell Code which deletes the List from SharePoint Online.
Help
help about_Methods -Detailed help about_Variables -Detailed help Read-Host -Parameter AssecureString help Add-Type -Detailed help Import-Module -Detailed |
PowerShell Code
#Import the required DLL Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll' Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll' #OR Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll' #Site URL $site = 'https://Domain.sharepoint.com/' #Admin User Principal Name $admin = 'Admin@Chensoffice365.OnMicrosoft.Com' #Get Password as secure String $password = Read-Host 'Enter Password' -AsSecureString #Get the Client Context and Bind the Site Collection $context = New-Object Microsoft.SharePoint.Client.ClientContext($site) #Authenticate $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($admin , $password) $context.Credentials = $credentials #Delete List $list = $context.Web.Lists.GetByTitle('PowerShell CSOM') $context.Load($list) $list.DeleteObject() $list.Update() |
Execute Query Method()
Why we don't use execute query method? Reason is once the Object is deleted using DeleteObject and Update method Execute query method is not required. It throws below error. We get this error because we loaded the list in client context and deleting the Object. So this is expected, the correct method is to delete list with out load method and call ExecuteQueryMethod.
$list.DeleteObject
$context.ExecuteQuery();
Exception calling "ExecuteQuery" with "0" argument(s): "List does not exist. The page you selected contains a list that does not exist. It may have been deleted by another user." At line:28 char:1 + $context.ExecuteQuery() + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : ServerException