Enable SP.JS in publishing sites for SP 2013


Hi,

In my previous post, I have explained the way to add CSOM on master page of any kind of site. But later on when testing same code with publishing site, I realized that sp.js is not loaded on publishing site by default.

So in this post I will discuss how to enable sp.js in Publishing Pages in SharePoint 2013. As the SharePoint uses minimal download strategy it prevents the page to have sp.js when published but the point to note is when you open your ribbon bar (edit or unpublished state) sp.js  script gets loaded in your page .

Note : SharePoint loads scripts on Demand
So to solve this we can do like this: Add a script editor webpart in your page and in script tags just add the following code snippet:
SP.SOD.executeFunc(‘sp.js’, ‘SP.ClientContext’, alert(‘I have loaded the sp.js script.’));
Where instead of alert method you can use any JS method and will be executed after sp.js is loaded.
Happy SharePointing :).
Advertisements

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