7


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("/");
createitem.executeAsync({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('MyList')/items('" + id + "')/AttachmentFiles/add(FileName='" + file.name + "')",
    method: "POST",
    binaryStringRequestBody: true,
    body: contents2,
    success: function (data) {
        //OK
    },
    error: function (msg) {
        alert("error");
    },
    state: "Update"
});
Nk SP
  • 2,893
  • 6
  • 39
  • 62
  • 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 will try to change it – Nk SP Apr 18 '14 at 14:10
  • Is @target hardcoded? – Nk SP Apr 18 '14 at 14:15
  • I posted an answer with my code. I hope it helps. – wjervis Apr 18 '14 at 14:20

1 Answers1

6

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);
    context.set_webRequestExecutorFactory(factory);
    appContextSite = new SP.AppContextSite(context, hostweburl);

    var executor = new SP.RequestExecutor(appweburl);

    executor.executeAsync(
        {
            url:
                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.

wjervis
  • 5,738
  • 24
  • 45
  • 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 08: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 09: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
  • Does the app have the appropriate permissions? I'd be able to provide more help later. Unfortunately I'm at work right now, so I can't provide detailed help right now. – wjervis May 26 '17 at 12:39
  • @wjervis yes, I have sitecollection scope at FullControl – michalh May 26 '17 at 12:47
  • @wjervis I'd appreciate greatly if you could have a look if you find some free time later... I'm really desparate – michalh May 26 '17 at 12:49
  • @MichalHainc If you post a question with the troubles you're having, you may get a response quicker. I'll be able to take a look at it in about an hour, when I go on my lunch break. – wjervis May 26 '17 at 14:16
  • @wjervis no stress... Im slowly starting to find out what is the issue... Angular2 router changes my url to other url than I entered the page with. Then this modified url is beeing used in the Refferer header when making JSOM proxified requests and I think this causes the error. Need to prove it with a blank solution without using the router – michalh May 26 '17 at 14:20