0

I am trying to access the list item using an APP in my office 365 share point hosted trial environment.It seems like the call REST call is failing, the alert in error function is getting executed.

This is the Url https://test.sharepoint.com/sites/AppSite/. I have created an APP called SharePointAppTest under AppSite web. There is a list called TestList created under Appsite web.TestList has an entry with ID as '1' and title as firstValue.

Below is the code I am using to access that item with ID using REST API

(function () {
    $(document).ready(function () {
        getListItem();
    });
    getListItem("https://test.sharepoint.com/sites/AppSite","TestList",1,"complete","failure");
})();

function getListItem(url, listname, id, complete, failure) {
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items(" + id + ")",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {              
            // Returning the results
            alert("it is a success");
            complete(data);
        },
        error: function (data) {
            alert("it is a failure");
            failure(data);
        }
    });
}
4

It appears that the list resides in Host web instead of the App web. In this case the app needs to be given permission to read the List of the host web. Moreover, the REST call needs to be done using cross domain library. Example of cross domain call using REST:

executor.executeAsync(
    {
        url:
            appweburl +
            "/_api/SP.AppContextSite(@target)/web/title?@target='" +
            hostweburl + "'",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: successHandler,
        error: errorHandler
    }
);

Read these for more information:

MSDN Article 1

MSDN Article 2

3

When you create or deploy app to the SharePoint it creates a web of its own(even your app may have list and libraries of its own) so app is nothing but a site in your SP, now when you try accessing the your site from this SP App SharePoint won't allow it due to security reasons, and you may get Access Denied error, So to overcome this you need to make a Cross Domain Call (a request from your App Domain/App web to your Site Domain/Host web)

For a app web you can access data as

 executor.executeAsync(
            {
              url:
                  appweburl +
                  "/_api/web/lists/getbytitle('Announcements')/items",
              method: "GET",
              headers: { "Accept": "application/json; odata=verbose" },
              success: successHandler,
              error: errorHandler
            }

While in case of a host web you need to change the context to the the context of Host web so you have to do it as below

executor.executeAsync(
{
    url:
        appweburl +
        "/_api/SP.AppContextSite(@target)/web/title?@target='" +
        hostweburl + "'",
    method: "GET",
    headers: { "Accept": "application/json; odata=verbose" },
    success: successHandler,
    error: errorHandler
}

See the difference in URL property of request you can change the context site by using the AppContextSite endpoint

find some Samples here

1
Problem is with crossdomain communication.
Sol : Load the SpExecutor,js from 15 Folder and perform all your operation with that.

Example:

$(document).ready(function () {
    SPAppWebUrl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
    SPHostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));

    // the cross-domain library is loaded
    $.getScript(SPHostUrl + "/_Layouts/15/SP.RequestExecutor.js");
    getItems_HostWeb();
});

// read URL parameters
function getQueryStringParameter(param) {
    var params = document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == param) {
            return singleParam[1];
        }
    }
}

var SPHostUrl; // the host web URL

// read the items from a list called Test in the host web
function getItems_HostWeb() {

    var url = SPAppWebUrl + "/_api/SP.AppContextSite(@target)" +
        "/web/lists/getbytitle('Employees')/items?" +
        "@target='" + SPHostUrl + "'";
    var globalvar;
    var executor = new SP.RequestExecutor(SPAppWebUrl);
    executor.executeAsync({
        url: url,
        method: "GET",
        headers: { "Accept": "application/json;odata=verbose" }, // return data format
        success: function (data) {
            // parse the returned data
            test(data);
            var body = JSON.parse(data.body);
            $("#dvMessage").html("Found " + body.d.results. + " items.");
        },
        error: function (data) {
            $("#dvMessage").html("Failed to read items.");
        }
    });
}

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.