none
Restoring multiple files using versioning.

    Question

  • Hi,

    I am currently in a situation where I need to restore multiple of files using Sharepoint Online versioning.

    I´ve got a script that should help me do this from the following URL provided by MS,

    https://gallery.technet.microsoft.com/Restore-previous-versions-bbcb0796#content

    The problem I´m having is that I need to narrow down to a specific folder and also search for a specific value (modified date, last changed by).

    Does anyone know/have any ideas how I could modify the CamlQuery to fit my situation as described above?

    Thanks in advance!

    James.

    function Get-SPOFolderFiles
    {
    param (
            [Parameter(Mandatory=$true,Position=1)]
    		[string]$Username,
    		[Parameter(Mandatory=$true,Position=2)]
    		[string]$Url,
            [Parameter(Mandatory=$true,Position=3)]
    		$password,
            [Parameter(Mandatory=$true,Position=4)]
    		[string]$ListTitle
    		)
    
    
      $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
      $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
      $ctx.Load($ctx.Web)
      $ctx.ExecuteQuery()
      $ll=$ctx.Web.Lists.GetByTitle($ListTitle)
      $ctx.Load($ll)
      $ctx.ExecuteQuery()
      $spqQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
     $spqQuery.ViewXml ="<View Scope='RecursiveAll' />";
       $itemki=$ll.GetItems($spqQuery)
       $ctx.Load($itemki)
      $ctx.ExecuteQuery()
    
      foreach($item in $itemki)
      {
     
      Write-Host $item["FileRef"] -ForegroundColor DarkGreen
      $file =
            $ctx.Web.GetFileByServerRelativeUrl($item["FileRef"]);
            $ctx.Load($file)
            $ctx.Load($file.Versions)
            try{
            $ctx.ExecuteQuery() }
            catch
            {
            continue;
            }
            if ($file.Versions.Count -eq 0)
            {
              $obj=New-Object PSObject
              $obj | Add-Member NoteProperty ServerRelativeUrl($file.ServerRelativeUrl)
              $obj | Add-Member NoteProperty FileLeafRef($item["FileLeafRef"])
              $obj | Add-Member NoteProperty Versions("No Versions Available")
    
              #$obj | export-csv -Path $CSVPath -Append
    
              Write-Output $obj
            }
            elseif($file.TypedObject.ToString() -eq "Microsoft.SharePoint.Client.File")
            {
              
              foreach ($vv in $file.Versions){
              Write-Host $vv.Created $vv.Size $vv.VersionLabel $vv.IsCurrentVersion  $file.Versions.Count
              
             # Write-Output $vv
              
              }
              if($file.Versions[($file.Versions.Count-1)].IsCurrentVersion)
              { 
              $vLabel=$file.Versions[($file.Versions.Count-2)].VersionLabel
              Write-Host "Version to be restored: " $vLabel
              }
              else{ $vLabel=$file.Versions[($file.Versions.Count-1)].VersionLabel
              Write-Host "Version to be restored: " $vLabel  }
              $file.Versions.RestoreByLabel($vLabel)
              $ctx.ExecuteQuery()
              }
    
    else
    {
    $obj = New-Object PSObject
    $obj| Add-Member NoteProperty FileLeafRef($item["FileLeafRef"])
              $obj | Add-Member NoteProperty Versions("No Versions Available")
      Write-Output $obj
      }
              <#
              try { $ctx.ExecuteQuery() 
              $obj=New-Object PSObject
              $obj | Add-Member NoteProperty ServerRelativeUrl($file.ServerRelativeUrl)
              $obj | Add-Member NoteProperty FileLeafRef($item["FileLeafRef"])
              $obj | Add-Member NoteProperty Versions($file.Versions.Count + " versions were deleted")
    
              $obj | export-csv -Path $CSVPath -Append
              }
              catch {
              $obj=New-Object PSObject
              $obj | Add-Member NoteProperty ServerRelativeUrl($file.ServerRelativeUrl)
              $obj | Add-Member NoteProperty FileLeafRef($item["FileLeafRef"])
              $obj | Add-Member NoteProperty Versions($file.Versions.Count + " versions. Failed to delete")
    
              $obj | export-csv -Path $CSVPath -Append
    
              }#>
            
    
    
            
      }
    
    
    
            
    
    
    
    }
    
    #Paths to SDK
    Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
    Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
     
    #Enter the data
    $AdminPassword=Read-Host -Prompt "Enter password" -AsSecureString
    $username="t@trial456.onmicrosoft.com"
    $Url="https://trial456.sharepoint.com/sites/teamsitewithlibraries"
    $ListTitle="uyyu"
    
    
    
    Get-sPOFolderFiles -Username $username -Url $Url -password $AdminPassword -ListTitle $ListTitle 

    Wednesday, January 25, 2017 11:45 AM

All replies

  • There's your CAML

    $spqQuery.ViewXml ="<View Scope='RecursiveAll' />";

    Here's your CAML query builder;

    https://spcamlqueryhelper.codeplex.com/

    Hope that helps.

    Wednesday, January 25, 2017 1:31 PM
  • Hi

    I will come back with a real example for you.

    Best regards


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Wednesday, January 25, 2017 4:51 PM
  • Hi

    This is the link  where you get the script

    Let me know if that help and if you need more customizations

    Best regards


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.

    Saturday, January 28, 2017 3:23 PM
  • Thank you very much,

    I will check it out and get back to you.

    Regards,

    James

    Monday, January 30, 2017 8:52 AM
  • Hi Romeo,

    Thank you again for the script!

    We are facing one problem with it and it is that it is exceeding the list view threshold which is 5000 files.

    Do you know if it is possible to go in to a specific folder and filter instead of pulling out all the files and then filtering them?

    Best Regards,

    James

    Monday, January 30, 2017 3:12 PM