Viewing Word, PowerPoint & HTML files stored in SharePoint in Power Apps v2 – cached PDFs

In an earlier blog I shared a technique Power App developers can use to display several types of documents stored in SharePoint in a Power App leveraging the PDF Viewer control. In essence this entails repurposing (aka hacking) the thumbnail URL property for a document such that the modified URL converts the original document to a PDF document which can be displayed in a PDF Viewer control in an app.

Having published my earlier blog roughly 18 months ago, I am glad to report that blog has become one of my most popular posts on this website. Based on some user comments I attribute the popularity to the simplicity of solution and equally sharing the showcase Power App demonstrated in the blog.

One user comment on the blog however identified a “bug” whereby a user has an instantance of the Power App running and another user (in SharePoint) may modify one of the documents is the corresponding Document Library.

In such scenerios one would expect that should the Power App user refresh the SharePoint Document Library data source, when selecting the updated document in the gallery the PDF Viewer control should by all accounts display the latest version of said document. However unfortunately it doesn’t for documents the user may have viewed the earlier version of in the same Power Apps session.

Display cached version of documents

With reference to the showcase Power App I shared in my earlier blog and the expression I used for the PDF Viewer control Document property, having done some further analysis it became apparent that when refreshing a SharePoint Document Library in the app wherein a document in the library had been modified, the URL used on the Document property of the PDF Viewer control for the modified document does not change when the data source is refreshed.

On inspection of the URL the reason it doesn’t change is because it contains a query string parameter “version%3dPublished” such that the latest published version of any given document will be generated (in this instance in PDF format).

Looking further into the web network traffic generated from the Power App when selecting different documents in the gallery and displayed in the PDF Viewer control I realised that Power Apps in fact caches the PDF document in the user’s web browser cache. Thus when a user modifies a document in SharePoint even when refreshing the data source in the app the thumnail/PDF URL does not change and hence the previous version of the document in the local web browser cache will continue to be displayed in the app.

In most scenarios this behaviour will likely be perfectly acceptable for users of the app with the exception being where documents in the library are frequently updated in SharePoint. In such cases users could potentially simply reload the app such that the thumbnail URLs will all change as a new access token (parameter in the URL) will be generated for the new app session. Per my previous blog this was in essense the logic showcased and implemented by using the following expression in the Document property of the PDF Viewer control:

Substitute(
    Home_DocumentLibrary_Gallery.Selected.Thumbnail.Small,
    "transform/thumbnail",
    "transform/pdf"
)

Display Latest Version Of Documents

Alternatively should you wish to ensure the latest version of any given document in the library is displayed in the PDF Viewer control when a user refreshes the data source with a relatively simple tweak this can be done.

As noted because Power Apps caches any given document displayed based on the URL specified in the Document property, and that that URL does not change when refreshing the data source, you can force the URL to change by appending a counter variable to the URL that increments by one each time the data source is refreshed.

That minor change to the “v1” expression for the Document property on the PDF Viewer control as shared in my previous blog will ensure the cached version is no longer a match for the new URL such that the latest PDF version of any given document in the library is generated and downloaded again.

Concatenate(
    Substitute(
        Home_DocumentLibrary_Gallery.Selected.Thumbnail.Small,
        "transform/thumbnail",
        "transform/pdf"
    ),
    Text(v_refresh_counter)
)

The only other changes needed to the original Power App previously shared are to initialize the refresh counter to 1 when the screen is displayed (OnVisible property) as well as to increment the refresh counter variable by 1 whenever the user refreshes the SharePoint Document Library data source (e.g. Refresh button).

Screen OnVisible
UpdateContext({ v_refresh_counter: 1 })

Refresh Button OnSelect
Refresh('SharePointDocumentLibrary');
UpdateContext({ v_refresh_counter: v_refresh_counter + 1 })

Wrapping Up

By all means download the slightly updated version of the app that now displays the most recent version of any given document in the SharePoint Library (when the data source is refreshed). As with the original version of the app, import it into your own environment, remove the existing data source connection and add a new data source connection to one of your own Document Libraries in SharePoint. Update the Items property on the Gallery control and set it the the data source connection you’ve added. And that’s about it.

GitHub – Viewing Word, PowerPoint and html files from SharePoint Document Libraries within PowerApps v2