Restore previous version in bulk using the CSOM

When you have a document library with versioning enabled it is hard to restore document in bulk. It is not possible to do this without writing code. You need to go to the version history overview and restore file by file. Kind of a long term process. You can do this with some CSOM code:

Each file contains a collection of FileVersion objects. Each FileVersion objects contains a label and by using that label you can restore your file. You must use the RestoreByLabel method on the Versions collection object.

The following code shows you how to restore previous version of a list of files:

 Sub Main()

        Dim caml As String = "CAMLSTATEMENT HERE"
        Dim camlQry As New CamlQuery()
        camlQry.ViewXml = caml

        Dim ctx As New Microsoft.SharePoint.Client.ClientContext("REPLACE BY YOUR SERVER URL")
        Dim list As List = ctx.Web.Lists.GetByTitle("Meetresultaten STM")
        ctx.Load(list)
        ctx.ExecuteQuery()

        If Not list Is Nothing Then
            Dim documents As ListItemCollection = list.GetItems(camlQry)
            ctx.Load(documents)
            ctx.ExecuteQuery()

            For Each document As ListItem In documents
                ctx.Load(document)
                ctx.ExecuteQuery()

                Dim docFile As File = document.File
                ctx.Load(docFile)
                ctx.ExecuteQuery()

                ctx.Load(docFile.Versions)
                ctx.ExecuteQuery()
                Dim v As FileVersion = docFile.Versions(docFile.Versions.Count - 1)
                ctx.Load(v)
                ctx.ExecuteQuery()

                If Not v Is Nothing Then
                    Console.WriteLine("FILE: {0} : {1} from: {2}", v.VersionLabel, v.Url, v.Created.ToShortDateString)
                    docFile.Versions.RestoreByLabel(v.VersionLabel)
                    ctx.ExecuteQuery()
                End If
            Next
        End If
        Console.WriteLine("Finished!")
        Console.ReadKey()
    End Sub