locked
SP.SOD in the Masterpage RRS feed

  • Question

  • In a SP Online in the masterpage, I have a JS file that calls the following in document.ready.

    SP.SOD.executeFunc('sp.runtime.js', null, function(){           

    SP.SOD.executeFunc('sp.js', null, function(){               

    SP.SOD.executeFunc('sp.publishing.js', null,GetNavigation);        

    });     });


    The code fails right in the first SP.SOD call.

    I'm I using wrongly the SP.SOD? is there a step by step example how to use the SP.SOD in JS file associated to a masterpage?

    Thank you



    Thursday, May 19, 2016 2:56 PM

Answers

  • Thanks to one of my colleagues, I found out that the correct way to use JS load with dependency, you must use the following:

    in the masterpage or pagelayout:


    <head>
    <script type="text/javascript" src="/_layouts/15/sp.init.js"></script>
    </head>

    Then, in the JS file that you are calling SP js or other JS files that you might need on demand with dependency:

     var scriptBase = _spPageContextInfo.webAbsoluteUrl + '/_layouts/15/';   

    SP.SOD.registerSod('SP.ClientContext', scriptBase + 'sp.js');   

    SP.SOD.registerSod('SP.UserProfiles', scriptBase + 'SP.UserProfiles.js');   

    SP.SOD.loadMultiple(['SP.ClientContext', 'SP.UserProfiles'], function ()

    {

    // your function here

    });





    • Edited by Joao P Costa Friday, July 1, 2016 11:02 AM code organizing
    • Marked as answer by Joao P Costa Friday, July 1, 2016 11:02 AM
    Friday, July 1, 2016 11:01 AM

All replies

  • Hi Joao,

    We can use SP.SOD.executeFunc function like below:

    var site;
    var siteUrl;
     
    function initCustom() {
        // independent calls here    
        
        // dependent calls here
        SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {    
            var ctx = new SP.ClientContext();
            site = ctx.get_site();
     
            ctx.load(site);
            ctx.executeQueryAsync(Function.createDelegate(this, OnSuccess),
                    Function.createDelegate(this, OnFail));
        });
    }

    And call the custom function using Jquery:

    $(Document).ready(initCustom);

    More information:

    Use SP.SOD.executeFunc(a,b,c) to load the SharePoint JavaScript libraries when needed

    Thanks

    Best Regards


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Friday, May 20, 2016 11:18 AM
  • Hi,

    which error does occure? I have to admit that I can't find a mistake in this code snippet.

    Friday, May 20, 2016 12:05 PM
  • Hello,

    That's the weird part. I get no error. When it arrives to the first SP.SOD line, there is no error, the JS just stops executing.

    I'm using this from the masterpage and not from a SharePoint app.

    Am I missing some reference in the masterpage so that the SP.SOD works?

    I followed the documentation from MS and with 0 results.

    Thank you
    Friday, May 20, 2016 12:23 PM
  • Did you take the default masterpage? We used the seattle masterpage with the default core.js script link, startscript tag and so one.

    We are using executefunc like you do, but not in dependency of each other. For the dependencycheck we are using the ExecuteOrDelayUntilScriptLoaded function. But that should not cause such problems.

    Friday, May 20, 2016 12:46 PM
  • Thanks to one of my colleagues, I found out that the correct way to use JS load with dependency, you must use the following:

    in the masterpage or pagelayout:


    <head>
    <script type="text/javascript" src="/_layouts/15/sp.init.js"></script>
    </head>

    Then, in the JS file that you are calling SP js or other JS files that you might need on demand with dependency:

     var scriptBase = _spPageContextInfo.webAbsoluteUrl + '/_layouts/15/';   

    SP.SOD.registerSod('SP.ClientContext', scriptBase + 'sp.js');   

    SP.SOD.registerSod('SP.UserProfiles', scriptBase + 'SP.UserProfiles.js');   

    SP.SOD.loadMultiple(['SP.ClientContext', 'SP.UserProfiles'], function ()

    {

    // your function here

    });





    • Edited by Joao P Costa Friday, July 1, 2016 11:02 AM code organizing
    • Marked as answer by Joao P Costa Friday, July 1, 2016 11:02 AM
    Friday, July 1, 2016 11:01 AM