1

I am trying to create one SharePoint hosted App on office 365.

I am trying to access the list item from host web, but it goes to error every time.

Please suggest me if i am missing something.

Please refer the following code i have written.

var hostweburl;
var appweburl;

$(document).ready(function () {

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

    var scriptbase = hostweburl + "/_layouts/15/";
    $.getScript(scriptbase + "SP.RequestExecutor.js", runCrossDomainRequest);

});

function runCrossDomainRequest() {
    var executor = new SP.RequestExecutor(hostweburl);
    executor.executeAsync(
        {
            url: hostweburl +"/_api/web/lists/getbytitle('Announcements')/items",
            method: "GET",
            headers: {
                "Accept": "application/json; odata=verbose",
                //"content-type": "application/json;odata=verbose",
                //"X-RequestDigest": $("#__REQUESTDIGEST").val()
            },
            success: successHandler,
            error: errorHandler
        }
    );
}

function successHandler(data) {
    alert("In Success");
    var jsonObject = JSON.parse(data.body);

}

function errorHandler(data, errorCode, errorMessage) {
    alert("could not complete cxoss domain call : " + errorMessage);
}
function getQueryStringParameter(paramToRetrieve) {
    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] == paramToRetrieve)
            return singleParam[1];
    }
}

1 Answer 1

0

I reviewed the code that looks like the mentioned at MSDN article Access SharePoint 2013 data from add-ins using the cross-domain library That should be working !!

So I think the issue is related to the APP permission, where you need to set the Site collection Scope to Read Permission as the following

  • In Solution Explorer > Double click on AppManifest to open it.
  • Click on the Permissions tab > At the Scope Column > Set Site collection to Read at Permission Column.

[Update]

Regarding SharePoint App Web URL vs SharePoint Host Web URL

  • SharePoint App Web URL is the site URL that the APP will be deployed.
  • SharePoint Host Web URL is the site URL that the APP will be installed.
6
  • Yes the code is same as mentiioned at msdn.microsoft.com/en-us/library/office/fp179927.aspx . Thanks a lot, i will check the APP permission as suggested. Commented Sep 18, 2016 at 5:50
  • Hope it works, Please tell me if you still face issue !! Commented Sep 19, 2016 at 10:51
  • Hi I have given the Read access to Site Collection. But still not working. Commented Sep 21, 2016 at 15:54
  • It started working when i change the URL as --> appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Announcements')/items?@target='" + hostweburl + "'", Commented Sep 21, 2016 at 15:55
  • I am totally new to sharepoint hosted app... Can you please explain me what is the difference between these two URL ? Commented Sep 21, 2016 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.