1

I am trying to access a list from a SharePoint hosted app.

I am following instructions in this article:

How to: Access SharePoint 2013 data from apps using the cross-domain library

According to the article, to access the list, I should use :

 appweburl + "/_api/web/lists/getbytitle('My List')/items"

I would say that I should use hostweburl instead as the list is hosted in My SharePoint site. Indeed, with appweburl I get a 404 error.

Anyway, using hostweburl doesn't solve. The function errorHandler return a message that the web app wasn't ditributed for this url.

Have you got any ideas or could you clarify which is the correct url to use?

  • Code above is perfectly working. I am experiencing issues with 'External List'like Gyonder. Any ideas on how can we access External list in hostweb from SharePoint hosted app using REST? – user34701 Oct 29 '14 at 21:27
3

You need to use SP.AppContextSite(@target) to make the REST Url in order to access hostweb list:

Here is an example:

function getHostWebListsUsingREST() {
    var executor;

    // although we're fetching data from the host web, SP.RequestExecutor gets initialized with the app web URL..
    executor = new SP.RequestExecutor(appweburl);
    executor.executeAsync(
        {
            url:
                appweburl +
                "/_api/SP.AppContextSite(@target)/web/lists/?@target='" + hostweburl + "'",
            method: "GET",
            headers: { "Accept": "application/json; odata=verbose" },
            success: onGetHostWebListsUsingRESTSuccess,
            error: onGetHostWebListsUsingRESTFail
        }
    );
}

See this for more info: http://www.sharepointnutsandbolts.com/2012/11/access-end-user-data-in-host-web-from.html

  • with your url the request is successful. The thing is that I want to access a specific list. And I am unauthorized to access my url list (a popup from the host asks username and password): var url = appweburl + "/_api/SP.AppContextSite(@target)" + "/web/lists/getbytitle('MY LIST')/items?" + "@target='" + hostweburl + "'"; – Gyonder Apr 15 '14 at 9:00
  • The app needs to be given access to host web. This can be done in App Mainifest file. – Nadeem Yousuf Apr 15 '14 at 9:11
  • The app has the List permission in AppManifest. Or is it something else I should grant? – Gyonder Apr 15 '14 at 9:14
  • If you are simply reading from a List then having read permissions on List is enough. – Nadeem Yousuf Apr 15 '14 at 9:19
  • I see that the problem is only with my list. With other list I've no problems. The thing about my list is that it come from an External Content Type. Maybe I should grant further permission to my app or the list?I've tried adding BCS permission to AppManifest but it still doesn't work. – Gyonder Apr 15 '14 at 9:41
1

If you are in an app you can use the following

use this code to get hostweburl and appweb url

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

function getQueryStringParameter(paramName) {
    var params = document.URL.split("?")[1].split("&");

    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");

        if (singleParam[0] === paramName) {
            return singleParam[1];
        }
    }
}

this is the query to get the list

$.ajax({
        url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('ListName')?@target='" + hostweburl + "'",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {  
        },
        error: function (data) {
        }
});
  • Request goes successfully, but the data array is empty – Vignesh Subramanian Nov 24 '16 at 11:44
0

You can use also _spPageContextInfo.webAbsoluteUrl to get the Url and $.ajax:

var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/" +
        "getByTitle('YourTitle')/items/";
    var requestHeaders = { "accept": "application/json;odata=verbose" };
    $.ajax({
        url: requestUri,
        async: false,
        contentType: "application/json;odata=verbose",
        headers: requestHeaders,
        success: function (data) {
            //manage data.d.results
        },
        error: function (msg) {
            alert(msg.responseText);
        }
    });

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.