none
How to get the current version details of SharePoint item using power-shell CSOM from remote server ?

    Question

  • Hello,

    My objective is to get the version history details at client machine using powershell.

    Script is ready, however Its not returning the current version of the file from the documetn library, It returns only all the previous versions.Code is as below:

    # ******* Variables Section ******************
    #Define these variables
    $loc = "D:\Scripts" # Location of DLL's
    $loginname = "myloginname"
    $siteUrl = "http://mysite url"
    $ListName ="Documents"
    $ReportFile = "D:\Scripts\Invoice_VersionHistory.csv"
    # 
    
    #Authentication Process
    Set-Location $loc
    Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll")
    Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll")
    Write-Host "Please enter password for $($siteUrl):"
    $pwd = Read-Host -AsSecureString
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    $ctx.Credentials = New-Object System.Net.NetworkCredential($loginname, $pwd)
    
    #get web object
    $web = $ctx.Web 
    $ctx.Load($web) 
    $ctx.ExecuteQuery() 
    
    #get list object
    $list = $web.Lists.GetByTitle("Documents");
    $ctx.Load($list)
    $ctx.ExecuteQuery() 
    
     
    #delete file if exists
    If (Test-Path $ReportFile)
     {
     Remove-Item $ReportFile
     }
    
     #Check if list exists
     if($list -ne $null)
     {
      #Get all list items
      $ListItems = $list.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
      $ctx.Load($ListItems)
      $ctx.ExecuteQuery()      
       
    
      #Write Report Header
      Add-Content -Path $ReportFile -Value "Item ID,Created at, Item Title, Version ID , Version Lable,Version Comment"  
    
      #Loop through each item
        $ListItems | ForEach-Object {
          
            $Item = $_          
    
            if($Item["Title"] -ne $null  ) {
              
                $ctx.Load($Item.File)
                $versions = $Item.File.Versions             
                $ctx.Load($versions) 
                $ctx.ExecuteQuery()                
                   
               
                 foreach($version in $versions)
                {               
                    #Get the version content
                    $VersionData = "$($Item["itemid"]),$($version.Created),$($Item['Title']),$($version.ID ),$($version.VersionLabel),$($version.CheckInComment)"
                    
                    # Add-Content -Path $ReportFile -Value $VersionData
                    Add-Content -Path $ReportFile -Value $VersionData
               }   
                         
                             
            }
           
        } 
      
       
    }
    
    Write-Host "Version history has been exported successfully!"


    Would you please let me know how can I get the version details  of an item with current version details as well ? I require to get the details of all minor and major version details.I wonder why its happening, but I have no clue ! What's missing/wrong in above script ?

    Thanks and Regards,

    Dipti Chhatrapati


    Dipti Chhatrapati


    Wednesday, March 22, 2017 1:03 PM

Answers

  • Try the below, by design SPFileVersions collection don't have current version. So get it from file object direct

    # ******* Variables Section ******************
    #Define these variables
    $loc = "D:\Scripts" # Location of DLL's
    $loginname = "myloginname"
    $siteUrl = "http://mysite url"
    $ListName ="Documents"
    $ReportFile = "D:\Scripts\Invoice_VersionHistory.csv"
    #Authentication Process
    Set-Location $loc
    Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll")
    Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll")
    Write-Host "Please enter password for $($siteUrl):"
    $pwd = Read-Host -AsSecureString
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    $ctx.Credentials = New-Object System.Net.NetworkCredential($loginname, $pwd)
    
    #get web object
    $web = $ctx.Web 
    $ctx.Load($web) 
    $ctx.ExecuteQuery() 
    
    #get list object
    $list = $web.Lists.GetByTitle("Documents");
    $ctx.Load($list)
    $ctx.ExecuteQuery() 
    
     
    #delete file if exists
    If (Test-Path $ReportFile)
     {
     Remove-Item $ReportFile
     }
    
     #Check if list exists
     if($list -ne $null)
     {
      #Get all list items
      $ListItems = $list.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
      $ctx.Load($ListItems)
      $ctx.ExecuteQuery()
    
      #Write Report Header
      Add-Content -Path $ReportFile -Value "Item ID,Created at, Item Title, Version ID , Version Lable,Version Comment"  
      #Loop through each item
        $ListItems | ForEach-Object {      
            $Item = $_          
            if($Item["Title"] -ne $null  ) {
                $f = $Item.File
                $ctx.Load($f)
                $versions = $f.Versions             
                $ctx.Load($versions) 
                $ctx.ExecuteQuery()                
                   
                $currentVersion = "$($Item["itemid"]),$($f.TimeCreated),$($Item['Title']),$($f.UIVersion ),$($f.UIVersionLabel),$($f.CheckInComment)"
                Add-Content -Path $ReportFile -Value $currentVersion
    
                foreach($version in $versions)
                {               
                    #Get the version content
                    $VersionData = "$($Item["itemid"]),$($version.Created),$($Item['Title']),$($version.ID ),$($version.VersionLabel),$($version.CheckInComment)"
                    
                    # Add-Content -Path $ReportFile -Value $VersionData
                    Add-Content -Path $ReportFile -Value $VersionData
               }                        
                             
            }       
        }   
    }
    Write-Host "Version history has been exported successfully!"


    Regards Senthamil

    Thursday, March 23, 2017 8:09 AM

All replies

  • Try the below, by design SPFileVersions collection don't have current version. So get it from file object direct

    # ******* Variables Section ******************
    #Define these variables
    $loc = "D:\Scripts" # Location of DLL's
    $loginname = "myloginname"
    $siteUrl = "http://mysite url"
    $ListName ="Documents"
    $ReportFile = "D:\Scripts\Invoice_VersionHistory.csv"
    #Authentication Process
    Set-Location $loc
    Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.dll")
    Add-Type -Path (Resolve-Path "Microsoft.SharePoint.Client.Runtime.dll")
    Write-Host "Please enter password for $($siteUrl):"
    $pwd = Read-Host -AsSecureString
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    $ctx.Credentials = New-Object System.Net.NetworkCredential($loginname, $pwd)
    
    #get web object
    $web = $ctx.Web 
    $ctx.Load($web) 
    $ctx.ExecuteQuery() 
    
    #get list object
    $list = $web.Lists.GetByTitle("Documents");
    $ctx.Load($list)
    $ctx.ExecuteQuery() 
    
     
    #delete file if exists
    If (Test-Path $ReportFile)
     {
     Remove-Item $ReportFile
     }
    
     #Check if list exists
     if($list -ne $null)
     {
      #Get all list items
      $ListItems = $list.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
      $ctx.Load($ListItems)
      $ctx.ExecuteQuery()
    
      #Write Report Header
      Add-Content -Path $ReportFile -Value "Item ID,Created at, Item Title, Version ID , Version Lable,Version Comment"  
      #Loop through each item
        $ListItems | ForEach-Object {      
            $Item = $_          
            if($Item["Title"] -ne $null  ) {
                $f = $Item.File
                $ctx.Load($f)
                $versions = $f.Versions             
                $ctx.Load($versions) 
                $ctx.ExecuteQuery()                
                   
                $currentVersion = "$($Item["itemid"]),$($f.TimeCreated),$($Item['Title']),$($f.UIVersion ),$($f.UIVersionLabel),$($f.CheckInComment)"
                Add-Content -Path $ReportFile -Value $currentVersion
    
                foreach($version in $versions)
                {               
                    #Get the version content
                    $VersionData = "$($Item["itemid"]),$($version.Created),$($Item['Title']),$($version.ID ),$($version.VersionLabel),$($version.CheckInComment)"
                    
                    # Add-Content -Path $ReportFile -Value $VersionData
                    Add-Content -Path $ReportFile -Value $VersionData
               }                        
                             
            }       
        }   
    }
    Write-Host "Version history has been exported successfully!"


    Regards Senthamil

    Thursday, March 23, 2017 8:09 AM
  • Thank you so much Senthamil,

    Your updates on script worked as I needed :)

    Thank you again !

    Best Regards,


    Dipti Chhatrapati

    Thursday, March 23, 2017 9:33 AM