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 2016 / SharePoint online.
The following prerequisites need to be executed before going for any operations using CSOM PowerShell on SharePoint sites.
- 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.
Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
- Initialize client context object with the site URL.
- 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.
- 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.
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.
- 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.
- 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.