1

We are using the OOTB REST api to access data from the Host Web App using JavaScript. The MySite has been set up under a separate Web App following the recommendations of Microsoft. This site needs to access lists and libraries from the Host Web App. However, since the My site is now under a different domain, it therefore no longer has access to the Host's REST API via JavaScript.

I'm not sure if I'm fundamentally missing something, or if this just isn't a very common scenario.

I have been following this article to preform a cross origin request.

//_spPageContextInfo.siteAbsoluteUrl = http://mysite.hostweburl.com
var scriptbase = _spPageContextInfo.siteAbsoluteUrl + "/_layouts/15/";
$.getScript(scriptbase + "SP.Runtime.js", function () {
    $.getScript(scriptbase + "SP.js", function () { 
        $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest); 
    });
});

function execCrossDomainRequest() {
    var hostweburl = "http://www.hostWebUrl.com";
    var executor;

    // Initialize the RequestExecutor with the app web URL.
    executor = new SP.RequestExecutor(_spPageContextInfo.siteAbsoluteUrl);

    // Issue the call against the host web.
    // To get the title using REST we can hit the endpoint:
    //      app_web_url/_api/SP.AppContextSite(@target)/web/title?@target='siteUrl'
    // The response formats the data in the JSON format.
    // The functions successHandler and errorHandler attend the
    //      success and error events respectively.
    executor.executeAsync({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/title",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: successHandler,
        error: errorHandler
    });
    executor.executeAsync({
        url: _spPageContextInfo.siteAbsoluteUrl + "/_api/SP.AppContextSite(@target)/web/title?@target='" + hostweburl + "'",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: successHandler,
        error: errorHandler
    });
}; 
// Function to handle the success event.
// Prints the host web's title to the page.
function successHandler(data) {
    var jsonObject = JSON.parse(data.body);

    document.getElementById("HostwebTitle").innerHTML =
        "<b>" + jsonObject.d.Title + "</b>";
}

// Function to handle the error event.
// Prints the error message to the page.
function errorHandler(data, errorCode, errorMessage) {
    document.getElementById("HostwebTitle").innerText =
        "Could not complete cross-domain call: " + errorMessage;
}

However I have receiving this error:

"{"error":{"code":"-1, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"There is no app context to execute this request."}}}"

My question: How can the 'app context' be set up for a MySite Web App in order for it to be able to access the Host Web App?

  • Have you tried simply using $.ajax()? – wjervis Jun 8 '15 at 16:52
  • @wjervis - In order to accomplish that, CORS would have to be enabled. MS's solution to this is to use the cross-domain library. – c_Reg_c_Lark Jun 8 '15 at 17:11
  • So you have not tried? I'm in O365, and can perform REST requests using $.ajax(). Granted, they may not follow their own advice. – wjervis Jun 8 '15 at 17:28
  • @wjervis - Sorry, yes I have tried: XMLHttpRequest cannot load hostweb/_api/SP.UserProfiles.PeopleManager/GetMyProperties. Invalid HTTP status code 403 – c_Reg_c_Lark Jun 8 '15 at 17:56
  • Also, this is an On Premise installation – c_Reg_c_Lark Jun 8 '15 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.