Article
0 comment

Load Display Templates from Provider Hosted App or remote website

Display templates can only be stored in several places of SharePoint to get loaded. I was wondering if I will be able to inject this mechanism or if I will be able to provide a display template directly out of a provider hosted app.

Normally Display Templates will be uploaded to every site collection inside the master page catalog, but with a little tweak those can be loaded from everywhere in the world as long they are accessible via a web server.

The local display template

Display templates will be executed not on the server, but on the client. Embedding remote scripts is something we do all the day. If we use jQuery or SPServices, for example.

To load a display template we just need to use the function:

At the end of this snippet all I defined the locations where the remote display template is located. In my case it is on my local web server. The only thing we do now is to upload this script to the display templates instead of the code for the display template.

Script for loading external display template inside Master Page Gallery

Display Template

This script we reference in the JSLink property of a list view web part, for example.

Reference display Template form gallery

In my case the path is “~sitecollection/_catalogs/masterpage/display template/loadExternal.js” .

The remote display template

Next up we need to add the code for the “real” display template. The following snipped just register a custom action on a document library.

Now let’s see if the custom display template is loaded.

Custom Action initialised by remote display template

There you go the custom action is loaded from the remote web site to the SharePoint site and it is registered correctly.

Conclusion

I think this remote loading of display templates is pretty useful during development or if you like to create a display template repository within your organisation. Those remote display templates are simply easier to maintain.

Another use case might be that you keep your display templates inside the provider hosted website and just provision the load of the external javascript to SharePoint. Once again, you can upgrade the display templates without upgrading the app. Especially if the app is hosted on multiple environments.

For me it speeded up the development process, but keep an eye on possible security issues. As cool as it is. Scripts can be easily hacked and replaced by malicious code if you embed it directly from the internet.