JavaScript, SharePoint

SharePoint Load Multiple Script Dependencies

I’ve been spending a lot of time working in the SharePoint client side object model (CSOM) and one of the things I initially struggled with was waiting for SharePoint client dependencies to load before my code starts executing.

SharePoint 2010 addressed this with the SP.SOD class where I’ve been using the executeFunc method (described on MSDN)

This example waits for the sp.js file to load and when the SP.ClientContext method executes, calls the customCode function.

$(document).ready(function () { 
 SP.SOD.executeFunc('sp.js', 'SP.ClientContext', customCode);
});

function customCode() { 
 // code goes here
}

Very handy, but I soon began to have multiple dependencies that I needed to cater for.

In SharePoint 2013 though, things get better with the SP.SOD.LoadMultiple function. I haven’t been able to find any MSDN article on it yet, but it’s pretty straightforward.

It takes an array of strings and a function callback. It will wait until each script specified in the array is loaded and then execute the function callback.

$(document).ready(function () {
 SP.SOD.loadMultiple(['sp.js', 'clientpeoplepicker.js'], customCode);
});

function customCode() {
 // code goes here
}

Awesome

Advertisements

2 thoughts on “SharePoint Load Multiple Script Dependencies

    1. You are correct. I have jQuery included in my masterpage so for me it is available. Hopefully this clarifies my post for other users

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s