Retrieve File Version Properties And Version Author Details On SharePoint Using CSOM PowerShell

Introduction

In this article, you will learn how to retrieve version properties and version author details of files present on SharePoint folders programmatically using CSOM with PowerShell on SharePoint 2013 / SharePoint online.

Steps Involved

The following prerequisites need to be executed before going for any operations using CSOM PowerShell on SharePoint sites.

  1. Add the references using the Add-Type command with the necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll.

    Add-Type –

    Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"

    Add-Type –

    Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"

  2. Initialize client context object with the site URL.
    1. $siteURL = ""  
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  

  3. If you are trying to access a SharePoint Online site then you need to set up the site credentials with the credentials parameter and load it to the client context. 
    1. #Not required for on premise site - Start  
    2. $userId = ""  
    3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString  
    4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  
    5. $ctx.credentials = $creds  
    6. #Not required for on premise site - End  

  4. If you are trying to access the SharePoint on premise site then the credentials parameter is not required to be set to the context but you need to run the code on the respective SharePoint Server or you need to pass the network credentials and set the context.
    1. #Credentials for on premise site - Start    
    2. $pwd = Read-Host -Prompt "Enter password" -AsSecureString    
    3. $creds = New-Object System.Net.NetworkCredential("domain\userid", $pwd)    
    4. $ctx.Credentials = $creds    
    5. #Credentials for on premise site - End  

Retrieve File Versions

  • Initialize the web object using the context and url.

  • Retrieve the folder using the web object and folder title.

  • Create caml query object and assign query xml to get the necessary file from the folder.

  • Get items with GetItems method using the above query.
    1. web = $ctx.Site.OpenWeb("/")  
    2. $folder = $web.Lists.GetByTitle("Documents")  
    3. $query = "<View><Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>filename</Value></Eq></Where></Query></View>"  
    4. $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery  
    5. $camlQuery.ViewXml = $query  
    6. $items = $folder.GetItems($camlQuery)  
    7. #$items = $folder.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())  
    8. $ctx.Load($items)  
    9. $ctx.ExecuteQuery()  

  • From the items collections, take the first item and get the necessary details. You can also use foreach loop to see information about multiple files.

  • Access the file, and then file versions. Load the file and version objects, and execute the query.
    1. $item = $items[0]  
    2. $file = $item.File  
    3. $versions = $file.Versions  
    4. $ctx.Load($file)  
    5. $ctx.Load($versions)  
    6. $ctx.ExecuteQuery()  
    7.   
    8. Write-Host $item["Title""Version Details"  

  • Then using foreach loop, get each and every version information. The properties which can be accessed are version label, file URL, created time, version id, current version flag, version file size, version comment, and author details.

  • To get the version author details, load the author object and execute with context to get information about author. The author details which can be accessed are name, email id, and login id.
    1. foreach($version in $versions){  
    2.     $ctx.Load($version)  
    3.     $ctx.ExecuteQuery()  
    4.     Write-Host "--------------------------------------------------------"              
    5.     Write-Host "Version Label      : " $version.VersionLabel              
    6.     Write-Host "Version File URL   : " $version.Url  
    7.     Write-Host "Version Created On : " $version.Created       
    8.       
    9.     Write-Host "Version ID         : " $version.ID  
    10.     Write-Host "Current Version?   : " $version.IsCurrentVersion  
    11.     Write-Host "Version File Size  : " $version.Size  
    12.     Write-Host "Version Comment    : " $version.CheckInComment  
    13.     $modifiedBy = $version.CreatedBy  
    14.     $ctx.Load($modifiedBy)  
    15.     $ctx.ExecuteQuery()  
    16.     Write-Host "File Modified By : "   
    17.     Write-Host $modifiedBy.Title  
    18.     Write-Host $modifiedBy.Email  
    19.     Write-Host $modifiedBy.LoginName  
    20.     Write-Host "--------------------------------------------------------"  
    21. }  
Summary

Here  you have learned how to retrieve version properties and version author details of a SharePoint file(s) present on folders using CSOM with PowerShell on SharePoint 2013 / SharePoint online sites.