Scenario: A web part page with multiple custom web parts that should act independently. These web parts share a lot of the same list data.

My current solution makes multiple calls to the REST API to retrieve virtually the same list data and I'd like to only make one call to all of the lists I need on page load and allow other web parts to make use of the data via a global variable, similar to _spPageContextInfo.

I was thinking the SOD framework could help, but haven't been able to get that working. Something like:

SP.SOD.registerSod("init.js", "/init.js");

Then each web part can use something like:

ExecuteOrDelayUntilScriptLoaded(function() {
  //do something with the data using global variable        
}, "init.js");

But only one call to the server is made to gather up all of the data the whole page needs.

To summarize, I'd like a centralized file that is responsible for getting the data using the REST API, only make one call to the server to get this data, store data in a global variable. Allow independent web parts to consume this data, checking to see if the data exists first, wait for it to be loaded if it isn't, then use the data.


I suggest to write your own module to do that.

 var _myGlobalContext = (function () {
   var queryData;
   var queryHandlers;

   function doQuery(handler) {

   return {
     doQuery: doQuery

The function doQuery should ensure it only makes its request once.

Put everyone calling doQuery in a list of handlers and call all of them if the data is available. If it is already loaded, just call the handler immediatly with the result of the query.

You can use Sharepoints SOD to load your module everywhere it is needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.