I deployed an App SharePoint hosted.
I am using SP.RequestExecutor and it is working fine on SharePoint premise.
but on SharePoint online it is not working.
I added the library using this: $.getScript("/_layouts/15/SP.RequestExecutor.js");
Can I use that library also in SharePoint online?

This is my CODE :

var createitem = new SP.RequestExecutor("/");
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('MyList')/items('" + id + "')/AttachmentFiles/add(FileName='" + file.name + "')",
    method: "POST",
    binaryStringRequestBody: true,
    body: contents2,
    success: function (data) {
    error: function (msg) {
    state: "Update"
  • What error are you receiving when it fails? – wjervis Apr 18 '14 at 13:56
  • Nothing, I try to access to the msg.body, msg.status but they are undefined – Nk SP Apr 18 '14 at 13:59
  • Where does it fail? The new SP.RequestExecutor("/"); or the createitem.executeAsync({...? – wjervis Apr 18 '14 at 14:03
  • It fails in the executeAsync, I can see the alert in the error callback. – Nk SP Apr 18 '14 at 14:05
  • I'm looking at a SP hosted app I have working for SPO, and I'm using the app web url to create the SP.RequestExecutor. My REST url is quite different as well: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Communication%20Log')/items?$select=Title,Date1&$orderby=Date1 desc&$top=5&@target='" + encodeURIComponent(hostweburl) + "'" hostweburl being the url for the sharepoint online site, appweburl being the app's url. – wjervis Apr 18 '14 at 14:06

I used this when I first created my app. First, you need both the app web url and the host web url:

var hostweburl = decodeURIComponent( getQueryStringParameter("SPHostUrl") );
var appweburl = decodeURIComponent( getQueryStringParameter("SPAppWebUrl") );

Then you need to load the SP.RequestExecutor.JS file, which I did differently than what was in the link:

var scriptbase = hostweburl + "/_layouts/15/";

$.getScript(scriptbase + "SP.Runtime.js",
    function () {
        $.getScript(scriptbase + "SP.js",
            function () { $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest); }

execCrossDomainRequest is the function in which I executed the cross domain request. In my function, I was retrieving the five most recent items from a list called "Communication Log":

function execCrossDomainRequest() {
    var context;
    var factory;
    var appContextSite;

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

    var executor = new SP.RequestExecutor(appweburl);

                appweburl +
                    "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Communication%20Log')/items?$select=Title,Date1&$orderby=Date1 desc&$top=5&@target='" +
                        encodeURIComponent(hostweburl) + "'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: successHandler,
            error: errorHandler

I'm sure you can piece the rest together. This was done when I was fairly fresh with both SharePoint apps and javascript, so it may be improved.

  • Thanks, I changed my url request (added @target).It is working fine on-premise, but it is not working on Online :/ – Nk SP Apr 23 '14 at 8:20
  • I created new SP.RequestExecutor("/") but it needs the appweburl parameter. Strange that it was working on-premise. Thanks – Nk SP Apr 23 '14 at 9:14
  • Why in your function you created context, factory and appContextSite instances when you didnt use them anywhere? – michalh May 26 '17 at 11:48
  • @MichalHainc See the last comment under the code block. I suppose I ought to update this answer when I have a chance. – wjervis May 26 '17 at 12:17
  • @wjervis Would be helpfull ... Im struggling with this for 2 days, I'd like to use the appContextSite & context objects to work with the JSOM and manipulate data on the hostWeb but all I am getting is Access denied – michalh May 26 '17 at 12:30

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.