SharePoint 2016/2013/Online: How to Optimize SharePoint Custom Pages Using HTML5 IndexedDB API
In this article, we will discuss another obvious performance issues with SharePoint Solutions involving a large volume of data transactions surfacing SharePoint Custom Pages.
This could become more prominent if we have strict governance in place and we are not allowed to make use of advanced server-side options (Custom Web Service End Point, MTA Enabled Modules etc.).
In one of the recent assignment, we came across a similar scenario where I need to crawl data from an external Web Service end Point and surface data on SharePoint Pages. Since the anticipated data volume was huge and traditional caching approaches like Cookies won't work due to size limitations.
In pursuit of the solution, .weI have gone through the “HTML5 Web Storage APIs” that allows you to setup an In-Browser Transactional Database System called “IndexedDB”.
Here is a quick introduction of IndexedDB for details we must recommend you to visit IndexedDB
“IndexedDB is a transactional database system, like a SQL-based RDBMS. However, unlike SQL-based RDBMSes, which use fixed-column tables, IndexedDB is a JavaScript-based object-oriented database. IndexedDB lets you store and retrieve objects that are indexed with a key; any objects supported by the structured clone algorithm can be stored. Operations performed using IndexedDB are done asynchronously, so as not to block applications.”
Let's also want to thanks to “Raymond Camden” for his detailed research on Storage Limits for IndexedDB and believe you must refer this link to understand the limits carefully before getting into concrete implementations.
Now let’s try to understand the implementation details by using the following diagram:
Solution Architecture Diagram & Explanation
https://howtodowithsharepoint.files.wordpress.com/2017/03/12.png?w=800
In this solution, the SharePoint Page will try to look for the required data in Local Indexed DB created to support this page. If data is not found in the local database, a page will issue the request for data from SharePoint List.
Since we are dealing with “100,000” Items present in SharePoint List, we made use of “REST API + OData Continuation” data access strategy to overcome SharePoint List Threshold Limits. This mechanism will access only 100 List Items at a time and it is safe to extend this limit up to 2000 items per fetch.
Each fetch will a JSON Object that will be persisted into Indexed DB as an individual record. Opt this strategy to reduce the page load time. If the number of items is not much you can add each item to a separate record.
Every subsequent data call will be automatically diverted to the local database as a primary source.
Additionally, we can add “Auto Refresh Modules” to keep the local database fresh with SharePoint List Changes and sync the changes with Indexed DB “Asynchronously”.
Ideally speaking for a complete solution “Auto Refresh Modules” are must to have.
So this all about execution summary for this solution.
Now let’s have look at implementation details as follows-
Created a SharePoint List with two columns and “100,000” Items added to it as shown below.
Demo
This list will be acting as data source for the page. In actual scenarios, this source could be a Web Service End Point which can provide voluminous data on demand.
https://howtodowithsharepoint.files.wordpress.com/2017/03/22.png?w=800
https://howtodowithsharepoint.files.wordpress.com/2017/03/32.png?w=800
Before getting into code let’s see how this Page will behave on execution. Demonstrating the page in action will be helpful later when we get a deep dive in code.
If we run the page we will see this page took about “3 minutes” to get execution completed.
The first execution cycle will include the following actions:
- Initialize IndexedDB Database
- Query SharePoint List
- Add REST API Response to IndexedDB
- Load page with data from IndexedDB
Since we are adding data to the store asynchronously, the overall application will remain functional even it is taking three3 minutes to complete.
https://howtodowithsharepoint.files.wordpress.com/2017/03/41.png?w=800
Following screen shot showing data adding to IndexedDB asynchronously
https://howtodowithsharepoint.files.wordpress.com/2017/03/51.png?w=800
We can also review the Indexed DB initialized as the part of this request using “Developer Tools or F12 Key” with in the browser as shown below-
https://howtodowithsharepoint.files.wordpress.com/2017/03/61.png?w=800
We can explore each item in the each of the JSON Object as shown below-
https://howtodowithsharepoint.files.wordpress.com/2017/03/71.png?w=800
Now refresh the page to see the execution again and we can see roughly “1 second” to complete the page request.
The subsequent execution cycle will include the following actions:
- Query IndexedDB for data
- Load page with data from IndexedDB
So we can see how we can trim the execution path by using a well-defined strategy.
https://howtodowithsharepoint.files.wordpress.com/2017/03/81.png?w=800
Code Analysis
Let’s do the code analysis to understand the concrete implementation.
In Step 1 we are enclosing some of the literals as variables and will refer theses variables later in the code
https://howtodowithsharepoint.files.wordpress.com/2017/03/9.png?w=800
In Step 2 we are checking if respective Indexed Database is initialized already or not and if not Initialize the Database. In this demos let’s call this database as “Products”
https://howtodowithsharepoint.files.wordpress.com/2017/03/10.png?w=800
In Step 3 “onsuccess” event handler will get executed and database object will get stored in a global variable “SharePointOptimization.sharePointStore”. This variable will be acting as a start point for all the operations on the database in future.
In Step 4 default error handling module is assigned as callback function to “onerror”, “onblocked”, “onabort” event handler
https://howtodowithsharepoint.files.wordpress.com/2017/03/111.png?w=800
In Step 5 we are querying SharePoint List using REST API
https://howtodowithsharepoint.files.wordpress.com/2017/03/121.png?w=800
In Step 6 we are making use of OData Continuation Techniques to overcome SharePoint List Threshold restrictions.
In this step, we also call “AddDataToStore” function that will add SharePoint List Items coming as JSON Object to the Local Indexed Database. It is important to recall that in this demo we are storing 1 JSON Object as 1 record in a database and each object contains information for 100 List Items.
https://howtodowithsharepoint.files.wordpress.com/2017/03/13.png?w=800
In Step 7 we are adding JSON Objects to IndexedDB. In order to do that we need to perform following operations-
- Initialize Transaction with Read Write Operation Permissions
- Get Handle on “Products” Database inside IndexedDB Data Stores
- Call asynchronous “add” method to add JSON Object to “Products” Store
In Step 8 we are calling “QuerySharePoint” function to query data from SharePoint List in case data is not available in Local Database.
https://howtodowithsharepoint.files.wordpress.com/2017/03/14.png?w=800
Steps 9, 10, 11 explains about “ReadSPStore” function where we will read the data from Local Data Store (IndexedDB)
In Step 9 following operations are performed-
- Initialize Transaction with Read Operation Permissions
- Get Handle on “Products” Database inside IndexedDB Data Stores
- Call asynchronous “count” method to get total number of JSON Object available in “Products” Store
In Step 10 following operations are performed-
- Check for get count request status
- If success Initialize Indexed DB Cursor by calling asynchronous “openCursor” function
In Step 11 following operations are performed-
- Check for get cursor request status
- If success read the record from IndexedDB and add to the local array variable
- Call “continue” function as long as there are items left in local store
- Once all data is read and save to the local array pass this array to “RenderUI” function to render this data on the interface as required
https://howtodowithsharepoint.files.wordpress.com/2017/03/15.png?w=800
In Step 12 we can plug any UI engine to produce more intuitive UI as applicable, for the sake of this demo we are writing out the Count of Store records * 100 (since each record contains 100 Items) to show the total number of items stored in the local store.
https://howtodowithsharepoint.files.wordpress.com/2017/03/16.png?w=800
Steps 13, 14, 15 show you a helper function to check if the local store contains required data or not. It helps to decide if we need to read data from Local Store or SharePoint List
“GetProductCount” function is quite similar to the “ReadSPStore” function except it perform a lesser number of operations
https://howtodowithsharepoint.files.wordpress.com/2017/03/17.png?w=800
In Step 16 we will initialize Local SharePoint Store by calling “InitializeSharePointStore” function
https://howtodowithsharepoint.files.wordpress.com/2017/03/18.png?w=800
In Step 17 we can see some of the UI elements to build a basic UI for this demo
https://howtodowithsharepoint.files.wordpress.com/2017/03/19.png?w=800
Point of caution
Before implementing this mechanism make sure you have identified all the compatibility issues around this corner.
Recommend you to refer the following site every now and then to make sure you are using features supported by the targeted browsers.
http://caniuse.com/#search=IndexedDb
https://howtodowithsharepoint.files.wordpress.com/2017/03/20.png?w=800
Since we have made use of artefacts which are compatible with SharePoint Online Development Guidelines so we can use this approach with pages hosted in SharePoint Online as well.
That is all for this demo.
Hope you find it helpful.