Tuesday, December 5, 2017

Offline SharePoint Online site!



What I've shown in the video is how you can use a SharePoint site to serve as an mobile app that works offline!

The magic happens when you first enter the SharePoint site (and have authenticated on it): a piece of code is registered that makes the website available offline (a so called serviceworker). Next to this the data (that is stored online in Google Firebase) is synced to your device as well.
Now you can go offline and still use the App/site. I've built it so you can add reactions to a vacancy which are stored locally on your device. When you go back online, these reactions are synced to a SharePoint library.

So what's the point you might ask. Well, setting up a SharePoint site to be a real (single page) App allows for a great user experience on mobile and desktop with which you could let users file expense reports, book holidays, search for colleagues by expertise, etc. etc. For now, the new modern SharePoint experience does not support full page apps or customizing page layouts/masterpage so it works fast, has full branding and has an optimal user experience. By overriding the normal SharePoint experience with a SPA all this is possible without any modification to SharePoint except setting the homepage to an html page and uploading some html, js and css files.

Using this technique of registering a serviceworker to allow offline use of a website has the following pros:
  • Your SharePoint App is usable offline.
  • You can use sharepoint capabilities like identity, profile, lists, search when online and sync offline data to the site when going back online.
  • You can design a great looking App on SharePoint that works perfectly on desktop and mobile phone.
  • It's a website sothere's no need to put your App in the Google or iOS stores.
  • You can provide access to the App to your whole organization, or to specific users. And also to external users through external sharing functionality in SharePoint! Everyone you want can enjoy the App.
  • If you decide you do want a full native App on Android or iOS, the code is easily packaged as a real App.
This solution also has the following downsides:
  • You cant serve the manifest.json that is required for a full Progressive Web App with SharePoint (it doesn't serve JSON files at all). No manifest means no custom splashscreen, custom icon, or automatic install popup when you first enter the site. So basically you can only get half a PWA.
  • The App is a website and so does not have access to more advanced functionality of your phone. If you want better integration on a phone you would need to include those capabilities in the App and package it as an Android and iOS App. 
  • Offline mode only works in Chrome and Firefox (or any browser that supports serviceworkers). Microsoft and Apple are still working on this feature for their browsers. 
  • You will probably want to build this as an SPFx webpart later. For now full page apps arent yet possible in SPFx. PowerApps is another possibility to support offline use of SharePoint, but it has it's downsides as well.
  • When you host this App on SharePoint anonymous access is not possible.
This App was built using Ionic Framework, which is a framework that is build on top of Cordova and allows you to build phone apps in Angular. It uses SharePoint and Google Firebase as backend. Next to this SP PnP JS is used to easily post data to and fetch data from SharePoint. An Azure Function was created to fetch the vacancies from a third party API and store them in Firebase. Firebase is not necessary of course, you could do everything with just SharePoint. But eventually this App will not be hosted on SharePoint :-)

The following 'tricks' were needed to get this working:

Ionic PWA
See a previous post of mine that details how to build a Progressive Web App using Ionic2.

Upload files
Use the trusty old Internet Explorer and Explorer view in SharePoint to upload your Ionic app's www folder to the root folder of your site. NB: If you want to use a different SharePoint site than your O365 root site, you would need to make all the links used in your app server-relative, i.e. /sites/yoursite/index.html.

No Cors fetching of files to cache
Turns out that the default serviceworker included with the Ionic templates gives an error when deployed to SharePoint. This is because fetching the files required for offline use from SharePoint results in a few redirects that eventually gives an error because of Cross Origin Request Sharing (the redirects go to different domains like microsoftonline.com). See this stackexchange post for my solution.

Online / offline detection 
I had some trouble correctly implementing the detection of going online or offline. The events would fire multiple times. This was due to the place where you registered for the events in your code and because Ionic seems to create multiple instances of pages. See this stackexchange post for my solution.

Let me know what you think about this post!


4 comments:

  1. Hi Jurgen, good stuff. I'd like to organize a meeting soon, as I am also working towards including serviceworkers and connecting to O365. I'll be in thouch! Btw, I'm looking into hosting my manifests on a public github site ;)

    ReplyDelete
  2. Glad you found the post Marc, it's based on your work as you can tell :-)

    ReplyDelete
  3. Howdy! Do you use Twitter? I'd like to follow you if that would be ok. I'm undoubtedly enjoying your blog and look forward to new posts.

    ReplyDelete