0

I am trying to get project list from my project portal using JavaScript. I wrote a script that should do the job:

var projects;

// Geting JS library responsible for Project API 
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "/_layouts/15/sp.js";
jQuery("body").append(script);
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "/_layouts/15/ps.js";
jQuery("body").append(script);

console.log('SP.SOD.loadMultiple(["sp.js", "ps.js"], callback); is executed.');
SP.SOD.loadMultiple(['sp.js', 'ps.js'], function(){
    console.log('sp.js and ps.js was loaded succesfully.');

    projContext = PS.ProjectContext.get_current();
    projects = projContext.get_projects();
    projContext.load(projects, 'Include(Name, Description, StartDate, Id)');

    projContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
});

function onQuerySucceeded () {
    console.log('Callback onQuerySucceeded in LoadProjectsProperties was fired.');

    var enumerator = projects.getEnumerator();
    while (enumerator.moveNext()) {
        var project = enumerator.get_current();
        console.log(project.get_name());
    }
}

function onQueryFailed () {
    console.log('Callback onQueryFailed in LoadProjectsProperties was fired.');
}

In console it shows

'SP.SOD.loadMultiple(["sp.js", "ps.js"], callback); is executed.'

unreachable code after return statement[Learn More] (on sp.ribbon.js)
TypeError: this.$D_2 (on cui.js)

and this message:

'sp.js and ps.js was loaded succesfully.'

is never displayed which means that callback for loadMultiple functions is never running. Everything is enclosed in jQuery ready function and jQuery no conflict mode.

Does anyone had similar problem? What is a solution to load both scripts before running a script?

1

Try it as below:

SP.SOD.executeFunc('sp.js', 'SP.Utilities.Utility.getLayoutsPageUrl', loadMyProjectFiles);

function loadMyProjectFiles(){

    // Getting JS library responsible for Project API 
    SP.SOD.registerSod('SP.ClientContext', SP.Utilities.Utility.getLayoutsPageUrl('sp.js'));
    SP.SOD.registerSod('PS.ProjectContext', SP.Utilities.Utility.getLayoutsPageUrl('ps.js'));
    SP.SOD.loadMultiple(['SP.ClientContext', 'PS.ProjectContext'], function(){

        console.log('sp.js has been loaded..');
        console.log('ps.js has been loaded..');
        console.log('sp.js and ps.js was loaded succesfully.');

        projContext = PS.ProjectContext.get_current();
        projects = projContext.get_projects();
        projContext.load(projects, 'Include(Name, Description, StartDate, Id)');

        projContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

    });
}


function onQuerySucceeded () {
    console.log('Callback onQuerySucceeded in LoadProjectsProperties was fired.');

    var enumerator = projects.getEnumerator();
    while (enumerator.moveNext()) {
        var project = enumerator.get_current();
        console.log(project.get_name());
    }
}

function onQueryFailed () {
    console.log('Callback onQueryFailed in LoadProjectsProperties was fired.');
}
  • Hi Gautam, Thank you for your answer! Unfortunately it gives the following error now: jQuery.Deferred exception: SP.Utilities.Utility is undefined @connect.concare.net/pwa/projects.aspx?... TypeError: SP.Utilities.Utility is undefined both on current page. Do you know why this is happening? I am using it as script editor web part on standard project.aspx page. – TomaszP Jan 24 '17 at 11:13
  • can you check the updated code ? – Gautam Sheth Jan 24 '17 at 11:17
  • try running this first SP.SOD.executeFunc('sp.js','SP.ClientContext',<answers code>); – Tanmay Jan 24 '17 at 11:17
  • Ah! you beat me to it :P – Tanmay Jan 24 '17 at 11:17
  • The solutions worked! Thanks a lot, can you provide me with some pages/courses about how SP.SOD functions/ JavaScript libraries work in SharePoint? I am new to this topic, and this is pretty hard for me. Thanks! – TomaszP Jan 24 '17 at 12:41

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.