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

1 Answer 1

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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.