1

I'm trying to load data of a SharePoint App into my new SharePoint App and got some problems here.

At first I tried to use to solve it with Ajax, but it doesn't worked and I've read that I could use the SP.RequestExecutor for solving Cross Domain Requests(CrossDomain because I got 2 Apps with their own web URL). So I tried to use the SP.RequestExecutor but are always running into the error handler.

var executor = new SP.RequestExecutor(URLFragenkatalog);

var requestURL = appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Fragenkatalog')/items?$select=Title,Kategorie,Fragentyp,Antwortmoeglichkeiten,Antwort,Id&@target='" + hostweburl +"'";

executor.executeAsync(
    {
        url: requestURL ,
        method: "GET",
        headers: { "Accept": "application/json;odata=verbose" },
        success: function (data) { ladeKategorien(data) },
        error: function (data) { console.log("ERROR REST LADE KATEGORIEN (SCHNITTSTELLE)") }
    })    

Did I forgot something here or do I have to modify the app manifest or something like this? (Of course I've included the SP.RequestExecutor.js)

PS: Is there a way to do this synchronous? And if ur solving it with AJAX i'd also like to hear it

  • Are both of these SharePoint hosted apps (new and old)? What are the domains involved? or at least are they different domains that are not the host web? – John-M Sep 29 '15 at 11:41
  • @John-M They are both SharePoint hosted apps and they both have their own appweburl. At the moment they are on the same host web, but that may change in the future, so i dont want to rely on that – Sebastian Sep 29 '15 at 13:09
1

Tried it with the authorization header but it still dont work, i think its maybe because i got some error when trying to install an app (some interface problem) and only get the app installed due to some kind of bug :D and then i wont be asked if i trust this app... I think that could be the reason

If you think that's your problem, try removing one of the permissions you set up in the Appmanifest.xml and "re-add" them and run the App again, and you will be able to trust it again.

  • SharePoint usually won't force you to re-trust an app you already trusted, but if you fool SharePoint that you added or removed any permissions you will have to re-trust it. – Coderap Sep 30 '15 at 12:40
  • No the problem is I was never asked to trust this app, because i cant install it on the SharePoint directly. I always get an interface error that the COM-Object of Type "Sytem.__ComObject" cant be converted to "VSLangProj.VSProject". Due to that the installation fails, but i can retry the installation process in SharePoint and then it will be installed without a problem. But there I never get asked if I want to trust the Application ... – Sebastian Oct 1 '15 at 6:28
  • @Sebastian Well that doesn't sound very good, I personally have never had a problem to install an app.. trusting it have been a problem sometimes but this is something else, I would create a seperate question for just that, as that probably is the reason why nothing else is working as it should. flies away – Coderap Oct 1 '15 at 6:32
0

I solved it like this, when I had the same problem, I used SP.WebProxy for my cross-domain call. I'm actually using it right now.

When calling a "http" from a "https" which sharepoint is, you can use SP.WebProxy as a middle man, see here

And don't forget to specify remote endpoints in the Appmanifest.xml

It looks like this in my default.aspx:

Example GET

<!-- Placeholder for the remote content -->
<span id="categories"></span>

<!-- Add references to the JavaScript libraries. -->
<script 
    type="text/javascript" 
    src="../_layouts/15/SP.Runtime.js">
</script>
<script 
    type="text/javascript" 
    src="../_layouts/15/SP.js">
</script>
<script type="text/javascript">

        (function () {
        "use strict";
        var examplevariable1 = "";
        var examplevariable2 = "";
        var examplevariable3 = "";

        var context = SP.ClientContext.get_current();
        var request = new SP.WebRequestInfo();
        request.set_url("example/api/savingsaccounts/1");
        request.set_method("GET");
        request.set_headers({ "Accept": "application/json;odata=verbose" });

        var response = SP.WebProxy.invoke(context, request);
        context.executeQueryAsync(successHandler, errorHandler);

        function successHandler(data) {
            if (response.get_statusCode() == 200) {
                var json = JSON.parse(response.get_body());
                var output = "<UL>";
                for (var key in json) {
                    if (json.hasOwnProperty(key)) {
                        examplevariable1 = (json[key].id); //ExampleJSONObject
                        examplevariable2 = (json[key].ExampleJSONObject);
                        examplevariable3 = (json[key].ExampleJSONObject);
                        call_a_method (examplevariable1, examplevariable2, examplevariable3);
                        document.getElementById("categories").innerHTML = examplevariable3; //example output
                    }
                }

            }
            else {
                var errordesc;
                errordesc = "<P>Status code: " +
                     response.get_statusCode() + "<br/>";
                errordesc += response.get_body();
                document.getElementById("categories").innerHTML = errordesc;
            }
        }

        function errorHandler() {
            document.getElementById("categories").innerHTML =
                response.get_body();
        }
    })();
</script>

Example PUT

ClosedId = some id i got from some other onclick

function RESTPUTCLOSED(c) {
    var dataObjectClosed = { 'status': 'Closed' }
    var context = SP.ClientContext.get_current();
    var request = new SP.WebRequestInfo();
    request.set_url("http://example/REST/_api/savingsaccounts/" + ClosedId);
    request.set_method("PUT");
    request.set_headers({ "Content-Type": "application/json; charset=utf-8" });
    request.set_body(JSON.stringify(dataObjectClosed));
    var response = SP.WebProxy.invoke(context, request);
    context.executeQueryAsync(successHandler, errorHandler);
}

If you need to authorize ( log in / confirm your access to the remote service)

add this:

request.set_headers({ "Authorization": "somekey_ASdkjsakdjaKSJDlksd" });

To generate the authorization key, follow Shauna's answer here

  • Thanks for your reply, I'm now running into Status code 401 (unauthorized). Did I need to set app permissions? Or is it usually enough when i trust the app. – Sebastian Sep 29 '15 at 7:11
  • 1
    @Sebastian Edited, If your getting status code 401, it's because you need an authorization key, because your browser doesn't know how to log in for you without the key, other than that just trust the app and you should be fine. – Dr Cox Sep 29 '15 at 11:30
  • @Sebastian You can use "Advanced Rest Client" - chrome extension, in order to generate the authorization key through logging in after adding " authorization " to the left column on headers and then pressing the right column ( textbox ) and there will appear a "construct" tooltip to the right, where you can log in and it will generate the key for you. – Dr Cox Sep 29 '15 at 11:32
  • 1
    @Sebastian here's a good link for you: stackoverflow.com/questions/20591770/… – Dr Cox Sep 29 '15 at 11:33
  • 1
    @Sebastian yeah you need to add an authorization header though, as your getting statuscode 401 (unauthorized). I'm not sure how your authorization works etc, but I'm getting that error when I forget to add the "Authorization": "thekeyvalueoftheauthorizationrandomletters" header ^^ – Dr Cox Sep 29 '15 at 13:32

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.