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,

1
  • 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 Commented Sep 9, 2017 at 18:35

1 Answer 1

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()

1
  • It works thanks! No need to use "SP.AppContextSite". Commented Nov 15, 2016 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.