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 ?

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

  • Thanks for instant reply Youssef . I need to do the same using REST only . – Rohit Kharade Sep 11 '17 at 7:20
  • You are welcome @RohitKharade you will find above updated answer with Rest API Call. – Youssef Moumen El idrissi Sep 11 '17 at 8:28

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.