Search code examples
csomproject-serversharepoint-jsomproject-online

Not getting any project with JSOM PS.ProjectContext get_projects


In a Microsoft Project Server web app I am trying to get the list of projects using JSOM.

I am following the code in these examples:

This is what I am running to test:

var projectContext = PS.ProjectContext.get_current();
var projects = projectContext.get_projects();
projectContext.load(projects, "Include(Name)");
projectContext.executeQueryAsync(IterateThroughProjects, handleError);

function IterateThroughProjects(response) {
    var enumerator = projects.getEnumerator();
    var i = 0;
    while (enumerator.moveNext()) {
        i++;
        var project = enumerator.get_current();
        console.log("Name: " + project.get_name());
    }
    console.log("Done. " + i + " projects found.");
}

function handleError(sender, args) { 
    console.log("Request failed: " + args.get_message()); 
}

The script runs without any error but the result is always Done. 0 projects found. even though there are published projects in the PWA.

This is the response I get from the server:

[
    {"SchemaVersion":"15.0.0.0","LibraryVersion":"16.0.4756.1000","ErrorInfo":null,"TraceCorrelationId":"fcde4d9f-9755-2050-31d1-da0833d8ce31"},
    362,
    {"_ObjectType_":"PS.ProjectCollection", "_Child_Items_":[]}
]

I have tried against Project Online, Project Server 2013 and Project Server 2016.

Is there any setting in SharePoint or PWA I am missing? Anyone with the same problem?


Solution

  • My problem was that I was running this code from a SharePoint App and the app manifest didn't include permissions for projects (http://sharepoint/projectserver/projects -> Read):

    <?xml version="1.0" encoding="utf-8"?>
    <App xmlns="http://schemas.microsoft.com/sharepoint/2012/app/manifest" ...>
        <Properties>...</Properties>
        <AppPrincipal>...</AppPrincipal>
        <RemoteEndpoints>...</RemoteEndpoints>
        <AppPermissionRequests>
            ...
          <AppPermissionRequest Scope="http://sharepoint/projectserver/projects" Right="Read" />
        </AppPermissionRequests>
    </App>
    

    After adding the permission I get the list of projects.