3

I am creating a SharePoint hosted app in which i am fetching a SharePoint list data and showing it on html page . Whenever i am making the rest API call , i am getting " Access denied. You do not have permission to perform this action or access this resource error".

enter image description here

I have full access on the list and SharePoint site as well . Can anybody please tell me why i am getting this error ?

2

1 Answer 1

1

If so, you need to use Cross Domain to overcome this issue apply this code below in your app.js

JSOM Method

'use strict';
var hostweburl, appweburl;
var myList;

$(document).ready(function () {
    appweburl = window.location.protocol + "//" + window.location.host + _spPageContextInfo.webServerRelativeUrl;
    hostweburl = _spPageContextInfo.siteAbsoluteUrl;

    var scriptbase = hostweburl + "/_layouts/15/";

    $.getScript(scriptbase + "SP.Runtime.js",
        function () {
            $.getScript(scriptbase + "SP.js", retrieveListItems);
        }
    );
});

function retrieveListItems() {
    var context = new SP.ClientContext(appweburl);
    var appContextSite = new SP.AppContextSite(context, hostweburl);
    myList = appContextSite.get_web().get_lists().getByTitle("yourListTitle");
    context.load(myList);
    context.executeQueryAsync(onGetWebSuccess, onGetWebFail);

    //the remaining code is omitted for clarity  
}

function onGetWebSuccess() {
    alert("calling" + myList.get_title());
}

function onGetWebFail(sender, args) {
    alert('Failed to get lists. Error:' + args.get_message());
}

Rest API Call

var executor; 
executor = new SP.RequestExecutor(appweburl); 
executor.executeAsync( { 
    url: appweburl + "/_api/SP.AppContextSite(@target)/web/Folders@target='" + hostweburl + "'", 
    method: "GET", 
    headers: { "Accept": "application/json; odata=verbose" }, 
    success: getSuccessHandler, 
    error: getErrorHandler 
}

For more details about Cross Domain follow this link

2
  • Thanks for instant reply Youssef . I need to do the same using REST only . Sep 11, 2017 at 7:20
  • You are welcome @RohitKharade you will find above updated answer with Rest API Call. Sep 11, 2017 at 8:28

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.