Sunday, August 7, 2016

SharePoint Online: Restore Previous Version of a Document using PowerShell

How to Restore a previous version of a document or Item in SharePoint Online?

  • Go to your SharePoint online document library and click on the ellipsis icon next to the document title.
  • From the context menu pop-up, Click on the ellipsis again and then choose "Version History" menu item.
  • In version history page, Hover your mouse over the "Modified" date of the version you want to restore and click the arrow icon to get its context menu.
  • Click "Restore" and confirm the prompt with Yes to restore the particular version of a document.
    powershell to restore previous version in sharepoint online
    SharePoint Online Restore Previous Version
SharePoint Online: Restore a Previous Version of All Items in a Document Library using PowerShell
While its straightforward and simple in SharePoint Online to restore previous version, lets restore previous version of all documents in a document library with PowerShell.
#Import SharePoint Online module
Import-Module Microsoft.Online.SharePoint.Powershell

Function Restore-PreviousVersion()
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $ListName
   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 all items from the list/library
        $Query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
        $List = $Ctx.Web.Lists.GetByTitle($ListName)
        $ListItems = $List.GetItems($Query)

        #Iterate through each item and restore previous version
        Foreach($Item in $ListItems)
            #Get the file versions
            $File = $Ctx.Web.GetFileByServerRelativeUrl($Item["FileRef"])

            If($File.Versions.Count -gt 0)
                #Get the previous version's label

                #Restore previous version
                Write-Host -f Green "Previous version $VersionLabel Restored on :" $Item["FileRef"]
                Write-host "No Versions Available for "$Item["FileRef"] -f Yellow
    Catch {
        write-host -f Red "Error Removing User from Group!" $_.Exception.Message

#Set parameter values

#Call the function to restore previous document version
Restore-PreviousVersion -SiteURL $SiteURL -ListName $ListName

Here is my another post for SharePoint On-Premises to restore previous version of a file using PowerShell: How to Restore Previous Version of a File using PowerShell

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...