0

I have 300 documents in a document library and i want to restore all documents from its old version.Each document has two version v 1.0 and v 2.0 is there any way to restore all documents from its old version 1.0 one time instead of selecting each document its take time.

i want to restore from its old version only documents created date less than 05/01/2017 or Filesize=1KB

1 Answer 1

1

You will need to automate this task using code. You can use CSOM to achieve this

VB: (you can use C# too)

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

Powershell:

$web = Get-SPWeb -Identity http://sp2010
$list = $web.Lists["Documents"]
$items = $list.Items
foreach ($item in $items)
{
    $file = $item.File
    $fileVersions = $file.Versions      
    if ($fileVersions.Count -gt 0)
    {           
        $fileVersions.Restore($fileVersions.Count - 1)          
    }   
}
6
  • added powershell code too Jan 15, 2017 at 11:22
  • You need to use SP Query. Jan 15, 2017 at 11:33
  • 1
    $spQuery = New-Object Microsoft.SharePoint.SPQuery; $camlQuery = "<Where><And><Geq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'>" + dtStartDate + "</Value></Geq>" + "<Leq><FieldRef Name='Created' /><Value Type='DateTime' IncludeTimeValue='FALSE'>" + dtEndDate + "</Value></Leq></And></Where>"; $spQuery.Query = $camlQuery $sourceItems = $sourceList.GetItems($spQuery) Jan 15, 2017 at 11:34
  • Make sure that the date is ISO format Jan 15, 2017 at 11:34
  • and also it has some documents versions 4 v1.0,v2.0,v3.0 ?
    – adilahmed
    Jan 15, 2017 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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