2

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.