“EnsureScriptFunc is Undefined” – How to Handle Javascript this exception in SharePoint 2013.


Sathish Nadarajan
SharePoint MVP
Published On :   28 Nov 2013
Visit Count
Today :  4    Total :   16902
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

SharePoint Office 365 Tool
Simple & Powerful Tool for Migration, Security & Reporting. Free Trial


Some time back, we saw how to do the SetRating using the Reputation.js File here. As a Recap, let us have a look into the code snippet.

 function SetRating() {
 
         itemID = 4;
 listID = “0F4BA645-5753-4526-A978-6BF2C0A2E654”
 RatingValue = 1;
 
         EnsureScriptFunc('reputation.js',
                 'Microsoft.Office.Server.ReputationModel.Reputation',
                 function () {
                     var ctx = new SP.ClientContext(“http://sathishserver:20000/sites/PublishingSite/”);
                     rating = Microsoft.Office.Server.ReputationModel.Reputation.setRating(ctx, listID, itemID, RatingValue);
 
                     ctx.executeQueryAsync(Function.createDelegate(this, this.RatingSuccess), Function.createDelegate(this, this.RatingFailure));
                 });
 
     }
 

On the above method, you can see a function named EnsureScriptFunc. When I added this in to a Script editor Web Part on any of the Site Pages, it worked perfectly. But, in one of our scenario, we were not using any of the MasterPages. The requirement is something like, we need to create an application page, that should not contain the default master page. Moreover, it should not contain any master page. We did that. Am planning to describe about that some time later.

On the blank Application Page, I wrote this method and deployed. When I was trying to do the actual rating, I was getting an exception like “EnsureScriptFunc is undefined”. While surfing, I could not find enough information regarding this. As a last option, I tried to find it out myself J.

By using the IE Developer tool, opened all the script files loaded along with the master page and searched for the Keyword EnsureScriptFunc. Atlast I found this on the Init.debug.JS. After adding that to the blank application page, it started working.

Hence, the conclusion here is, EnsureScriptFunc is declared in the Init.Debug.JS file. If we experience the exception again, we need to check for the Init.Debug.JS.

Happy Coding.

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

SharePoint Analytics