I have a document library where i have uploaded documents it has 2 major versions and i want to delete the first major version. Is there powershell csom code i can used to do this
1 Answer
Connect to your SharePoint Site
# Bind to site
#
$Context = New-Object Microsoft.SharePoint.Client.ClientContext("Your URL")
$Credentials = Get-Credential -Message "Enter your credentials for SharePoint Online:"
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Credentials.UserName,$Credentials.Password)
# Get Web Object
#
$Web = $Context.Web
$Context.Load($Web)
$Context.ExecuteQuery()
Get Object of the uploaded document
$Upload = $Web.GetFileByServerRelativeUrl($fileUrl)
$Context.Load($Upload)
Get versions of file and delete them
# Get versions and delete
$Version = $Upload.Versions
$Context.Load($Version)
$Context.ExecuteQuery()
#[0] is the first version, and its ID is 512
$Version[0].DeleteObject()
$Context.ExecuteQuery()