none
PS.ProjectContext from JavaScript not working in O365 RRS feed

  • Question

  • Hey all,

    I have a O365 SharePoint tenant with a site("/sites/mr") as well as a Project Web App site ("/sites/pwa") and I'm trying to access the PWA programmatically from the JavaScript CSOM. I have PS.js installed and can access the namespace, but when I try to create a context I get an error:

    var obj = new PS.ProjectContext("/sites/pwa");

    Uncaught TypeError: undefined is not a function sp.runtime.js:2

    SP.ObjectPath sp.runtime.js:2

    Type.initializeBase msajaxbundle.js:1

    SP.ObjectPathConstructor sp.runtime.js:2

    PS_ProjectServer$newObject  PS.debug.js:11197

    Any thoughts?

    Thanks,

    Adam

    Tuesday, March 24, 2015 7:58 PM

All replies

  • Adam,

       As far as I can see, the constructor that accepts the URL should work.  I suspect it might be a script loading issue - or a conflict between using multiple contexts at once.  If I were you I would temporarily move the javascript code to the PWA site and change the first line to:

    var obj = PS.ProjectContext.get_current();

    This will eliminate the constructor as the problem.  If you still have a problem getting and using the context then you know the problem lies elsewhere - perhaps with the script load order, or a timing issue - i.e. your script is executing before other scripts have finished loading.

    Good luck - and let us know what the issue was.

    Cheers,

       James.


    James Boman BSc. MCP:EAD -

    Wednesday, March 25, 2015 1:14 AM
  • Hey James,

    Same error when run from the PWA site. I'm running this from the console (for testing purposes) after the page has loaded, so it seems unlikely that script timing is at fault.

    Wednesday, March 25, 2015 7:26 PM
  • I tried this from the Chrome console on the Project Center page - but it looks that the ProjectContext object is not loaded by default on all pages.

    Here is some known good working code for you to get started with.

    Just paste this into a "Script Editor Webpart" and then okay, stop editing, and re-navigate to the page from a menu.

    Once you confirm this is working, transport it to the other site and change the constructor.

    Cheers,

       James.

    <table width="100%" id="tblProjects">
            <tr id="headerRow">
                <th width="40%" align="left">Project ID</th>
                <th width="30%" align="left">Project name</th>
                <th width="30%" align="left">Created date</th>
            </tr>
        </table>
        <div id="divMessage">
            <br />
            <span id="spanMessage" style="color: #FF0000;"></span>
        </div>
    
        <script type="text/javascript">
    
            //safely load JQuery
            if (typeof jQuery == 'undefined') {
                var headTag = document.getElementsByTagName("head")[0];
                var jqTag = document.createElement('script');
                jqTag.type = 'text/javascript';
                jqTag.src = "//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js";
                jqTag.onload = myJQueryCode;
                headTag.appendChild(jqTag);
            } else {
                myJQueryCode();
            }
    
    
            //Do stuff with jQuery
            function myJQueryCode() {
                $(document).ready(function () {
                    $.getScript("/_layouts/15/ps.js")
                      .done(function (script, textStatus) {
                          //Get all the projects
                          GetProjects();
                      });
                });
            }
    
            // Declare a variable to store the published projects that exist
            // in the current context.
            var projects;
    
            // Get the projects collection.
            function GetProjects() {
    
                // Initialize the current client context.
                var projContext = PS.ProjectContext.get_current();
    
                // Get the projects collection.
                projects = projContext.get_projects();
    
                // Register the request that you want to run on the server.
                // This call includes an optional "Include" parameter to request only the
                // Id, Name, and CreatedDate properties of the projects in the collection.
                projContext.load(projects, 'Include(Id, Name, CreatedDate)');
    
                // Run the request on the server.
                projContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);
            }
            function onQuerySucceeded(sender, args) {
    
                // Get the enumerator and iterate through the collection.
                var projectEnumerator = projects.getEnumerator();
                while (projectEnumerator.moveNext()) {
                    var project = projectEnumerator.get_current();
    
                    // Create the row for the project's information.
                    var row = tblProjects.insertRow();
    
                    // Insert cells for the Id, Name, and CreatedDate properties.
                    var cellId = row.insertCell();
                    cellId.innerHTML = project.get_id();
                    var cellName = row.insertCell();
                    cellName.innerHTML = project.get_name();
                    var cellCreatedDate = row.insertCell();
                    cellCreatedDate.innerHTML = project.get_createdDate();
                }
            }
            function onQueryFailed(sender, args) {
                $get("spanMessage").innerHTML = 'Request failed: ' + args.get_message();
            }
        </script>


    James Boman BSc. MCP:EAD -

    Wednesday, March 25, 2015 10:18 PM