Get and open historical versions of SharePoint files in Canvas Apps

Doing anything with Canvas Apps and SharePoint document libraries? Awesome! This is a pretty good way to restrict what your users can do in terms of manipulation of files and data. SharePoint would tend to give users too much control and access to change… READ MORE [https://lewisdoes.dev/blog/ge
Get and open historical versions of SharePoint files in Canvas Apps
exc-62df17c2bf576b4e383a4f8d
In: Low Code Lewis Content 🚀

Doing anything with Canvas Apps and SharePoint document libraries? Awesome! This is a pretty good way to restrict what your users can do in terms of manipulation of files and data. SharePoint would tend to give users too much control and access to change in comparison to how restricted we can make things in a Canvas app… which is as much as we like! BUT… it’s important to remember what a neat piece of software SharePoint online is for document management… this isn’t easily replicable in Power Apps. For example, SharePoint allows us to restore historical versions of a file when changes have been made. Wouldn’t it be GREAT, if we could do this in Power Apps?

In this post, I’m going to show you how we can retrieve the version history records of a particular document using a Power Apps triggered flow, then I’ll show you how we can use the Launch() function to open an older version of a file from a SharePoint document library!

Getting the data

The first step we need to take in providing end users with file version history in Power Apps is to retrieve the data around versions. We can do this using a web api and the HTTP request step in Power Automate specifically for SharePoint. The great part about this is we don’t need a premium license, whereas if we were using an api other than the one for SharePoint, we’d need a premium license for these calls.

Check out Pieter Veenstra’s post on SharePains to see how you can get this data for a SharePoint document library!

This post will also explain how you can get your data into the app and display it in a gallery using a split to separate the array into lines as records, and then another split to display each column of data separately in a gallery.

So now we have a gallery with our version history data in it. But users can’t interact with it yet… we want users to be able to open the historical versions of their files directly from our application.

Opening the version

So, in order to get the version of the file to open, we’ve actually got a pretty easy task to! Simply add a button to your gallery, so that it appears on every record and change the text to something like ‘view’ or ‘open’.

Now, if we head to the version history of a file in a document library within SharePoint, we can take a look at the behaviour we see when we both hover over the link to open a historical version, and the behaviour when we click the link.

Observation 1

So, for the first link in the version history, we’re getting the current version. This means the link is slightly different to that of a historical version, and it is just the standard link of opening the document.

Observation 2

For historical versions the link follows a format like this:

https://sharepointdomain.sharepoint.com/sites/sitename/_vti_history/versionnumber/DocumentLibraryName/Filepathwithextension

So now we have a link format for the current version of the document, and a different format for a historical version. This means we’re going to need to ignore or hide the first gallery item in Power Apps as we’re only wanting to get historical versions with the one link here. The second type of link which is meant for historical versions will not work for the current version.

Opening the file from Power Apps – Launch() OnSelect

So the next part is simply launching the link from Power Apps. But of course we do have some string manipulation to do here to ensure we have the right version number filled in and the right file path.

This is the syntax I am using:

Set(varSite,GetSiteEnvironmentVariable.Run(Index(Split(ThisItem.Result,"###").Result,1).Result, varSelectedWorkingDocumentFullPath).siteurl); Launch(varSite, {},LaunchTarget.New)

Here I am doing a number of things to factor some different things in. As I have a need to use an environment variable as part of my link, I’m actually using compose actions in a flow here which is run on the click of my button. I then set the link retrieved and passed back to my app from my flow to a variable called ‘varSite’. This also includes the file path which I passed into my flow to put on the end of the link, which is then passed back as a full link.

Now next part of my syntax simply launches my link taking the value from my global variable.

If you don’t need to use an environment variable to get your site url for SharePoint you can follow this syntax:

Launch(“https://sharepointdomain.sharepoint.com/sites/sitename/_vti_history/” & Index(Split(ThisItem.Result,”###”).Result,1).Result & “/” & Gallery.Selected.’Full Path’)

Here we’re almost doing a bit of a concatenation just without the CONCATENATE function. We’re saying, take my SharePoint link and the needed _vti_history part, then we’re adding the version history number from the result we’ve received back from our array of version history. We do this through splitting the data up in the same way we do for the version number column. Then we add another forward slash and put the full path of the file with the extension. I take this from another gallery where I’m selecting my document. For background, I’m opening my version history data based on a selected record in that gallery of documents.

Written by
Lewis Baybutt
Microsoft Business Applications MVP • Power Platform Consultant • Blogger • Community Contributor • #CommunityRocks • #SharingIsCaring
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to LewisDoesDev.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.