Skip to main content

Optimize SharePoint REST API with client side caching | Angular JS

If you working with any REST API and having the requirement of getting too many data from the from the DB or lists (in SharePoint) [GET requests]. You may facing slowness in the App, Web Part or in client side web part.

To overcome above issue with the load, one option would be caching of recurring data while you made all other performance improvement points. Following describes how yo improve performance of SharePoint Online/On-prem application which relies on REST API by using caching and uses Angular JS for client side development.

Note: Out of the box (OOTB) Angular JS having caching implementation on the $http, but in following sample I have used "angular-cache" since it give more features such as access to local storage and session storage.

First you should include following references https://github.com/jmdobry/angular-cache/tree/master/dist and include those it in your project.

Secondly, you should configure the caching options. Either you configure caching globally as below on the Angular app.js

[code language="javascript"]
//If need to apply all HTTP requests globally

(function () {
'use strict';

// create app
var app = angular.module('app', [
, 'angular-cache'
]);

// startup code
app.run(['$http', 'CacheFactory', function ($http, CacheFactory) {
//cache configs
$http.defaults.cache = CacheFactory('nylFileData', {
maxAge: 15 * 60 * 1000 // Items added to this cache expire after 15 minutes
, cacheFlushInterval: 60 * 60 * 1000 // This cache will clear itself every hour
, deleteOnExpire: 'aggressive' // Items will be deleted from this cache when they expire
, storageMode: 'localStorage' // This cache will use `localStorage`.
});
}]);
[/code]

Further configurations http://jmdobry.github.io/angular-cache/#configuration-options

Else you can define configurations per Angular service or may be per request.

[code language="javascript"]
(function () {
'use strict';
var serviceId = "myService";

angular
.module('app')
.factory(serviceId, ['$http', '$q', 'CacheFactory', myService]);

function myService($http, $q, CacheFactory) {
// init factory
init();

var service = {
getData: getData

};

return service;

//initialization
function init() {
//Cache initialization
CacheFactory('cacheData', {
maxAge: 15 * 60 * 1000 // Items added to this cache expire after 15 minutes
, cacheFlushInterval: 60 * 60 * 1000 // This cache will clear itself every hour
, deleteOnExpire: 'aggressive' // Items will be deleted from this cache when they expire
, storageMode: 'localStorage' // This cache will use `localStorage`
});
}
}
})();
[/code]

Finally, you should should bind your cache with the request.

[code language="javascript"]
//get data function
function getData() {
var deferred = $q.defer();
var start = new Date().getTime();

//request
$http({
method: 'GET',
cache: CacheFactory.get('cacheData'),
url: webUrl + '/_api/web/lists/getByTitle(\'My List\')/Items',
headers: {
'Accept': 'application/json;odata=verbose',
'Content-Type': 'application/json;odata=verbose'
}
}).then(function successCallback(data) {
// when the response is available
deferred.resolve(data);
console.log('Time to retrieve data - [' + (new Date().getTime() - start) + 'ms]', data, serviceId, false);
}, function errorCallback(error) {
// or server returns response with an error status.
deferred.reject(error);
});

return deferred.promise;
}
[/code]

Comments

Popular posts from this blog

Turn off/ Hide Details panel/ Information Panel on modern SharePoint lists

Not always we require to show changes done by other which is a mandatory feature in SharePoint online. 
















What is details pane (aka. Information Pane)? Detail pane/ Information shows information regarding the document if you selected a one or its showing recent changes within a list or library.

Follow linkto Microsoft documentation about details pane.

Bad news: Until Microsoft listen to User Voice, there is no straightforward way to enable disable this even you don't want.
Good news: We could write a SharePoint framework extension to hack styles until Microsoft give us a permanent solution.

How? I found this sample project (Inject CSS into modern SharePoint pages with React) which could reuse to our purpose. Thanks to Hugo for saving my time.  Steps to awesomeness: Clone the projectResolve dependencies > npm iBundle > gulp bundle --shipPackage > gulp package-solution --shipUpload package into SharePoint App catalog and deploy. You could find package under SharePoint folder.A…

PowerApps Delegation warning

Warning:Delegation warning. This part "Filter" of this formula might not work correctly on large datasets. The data source might not be able to process the formula and might return an incomplete data set. Your application might not return correct results or behave correctly if the data set is incomplete
"Working with large datasets requires using data sources and formulas that can be delegated" - PowerApps 










Solution!!!500 is the default number of records to pull, but you can change this number for an entire app. 

By altering the number of delegation queries (not recommended), you will be able to pull items up to 2000 (as of 2018). 
On the File tab, select App settings.Find Experimental features, change the Data row limit for non-delegable queries setting from 1 to 2000.










Data sources that will help with delegation:Common Data Service

📢 Update -Microsoft Teams IP Phones and Intune Enrollment

For customers who require desk phones and conference room phones to make and receive audio calls or join meetings, Microsoft Teams provides a growing portfolio of devices that can be purchased from our Teams Marketplace. For Teams phones including the Yealink T56A/T58A/CP960 and the Crestron Flex series IP phones that run on Android 5.x or later, there may be specific configurations that need to be enabled in the customer's tenant for the phones to successfully enroll into Intune. Allowing successful Intune enrollment for Android versions 5.x and upIf all the following conditions below are true, you will need to enable a specific configuration setting in the Intune admin console to allow for a successful enrollment:You are deploying a Teams IP phone with Android OS version 5.x or later.You have connected your Intune tenant with managed Google Play in order to manage Android Enterprise devices.You have configured your enrollment restrictions such that Android work profile enrollmen…