Error in SharePoint Hosted App
-
01-01-2021 - |
문제
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".
I have full access on the list and SharePoint site as well . Can anybody please tell me why i am getting this error ?
해결책
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
제휴하지 않습니다 sharepoint.stackexchange