4

I'm trying to get some project data using the REST API. My queries work fine when using Postman. But when it comes to SharePoint-Hosted app, I'm getting a "404 (Not Found)" error. My app is deployed on the PWA root web. Here is my code:

function GetProject() {

        var deferred = $.Deferred();

        JSRequest.EnsureSetup();

        var hostweburl = decodeURIComponent(JSRequest.QueryString["SPHostUrl"]);
        var appweburl = decodeURIComponent(JSRequest.QueryString["SPAppWebUrl"]);

        var context = new SP.ClientContext(appweburl);
        var appContextSite = new SP.AppContextSite(context, hostweburl);

        var restQueryUrl = appweburl + "/_api/SP.AppContextSite(@target)/ProjectServer/Projects?@target='" + hostweburl + "'";

        var executor = new SP.RequestExecutor(appweburl);
        executor.executeAsync({
            url: encodeURI(restQueryUrl),
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: function (data, textStatus, xhr) {
                deferred.resolve(JSON.parse(data.body).d);
            },
            error: function (xhr, textStatus, errorThrown) {
                deferred.reject(JSON.parse(xhr.body).error.message.value);
            }
        });
        return deferred;
};

Would you have some inputs to solve my problem? Thanks.

Regard,

  • I can't get it to work using Project Server REST API in SharePoint Hosted App. Can you send me the code that worked for you ? var projectContext = PS.ProjectContext.get_current(); var appContextSite = new SP.AppContextSite(projectContext, hostweburl); var restQueryUrl = appweburl + "/_api/ProjectServer/Projects('" + projectUid + "')/Name"; var executor = new SP.RequestExecutor(appweburl); executor.executeAsync({ url: encodeURI(restQueryUrl), method: "GET", headers: { "Accept": "application/json; odata=verbose" }, success: function (data, textStatus, xhr) { alert("success:" + JSON.parse(data.bo – Sunil Sep 9 '17 at 18:35
3

From what I see from samples here, they are using current web and not host web. From my understanding for Project it doesn't matter.
So you can try to call directly

https://yourorg-d2fcea9f69347f.sharepoint.com/sites/pwa/SharePointAddIn/_api/ProjectServer/Projects

Or use ps.js and PS.ProjectContext.get_current()

  • It works thanks! No need to use "SP.AppContextSite". – a1mery Nov 15 '16 at 8:06

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.