1

I am making a simple website where I have to save some of my data to sharepoint list. I am using only html css js only. I need to make REST calls to Sharepoint APIs to post data on SP. I am trying to get data from my list(in SP) as follow:

$(document).ready(function() {
        processListItems(hostweburl, 'ListName');
    });

function processListItems(url, listname) {

        $.ajax({
            url: url + "/_api/web/lists/getbytitle('" + listname + "')",
            method: "GET",
            headers: {
                "Accept": "application/json; odata=verbose"
            },
            success: function(data) {
                console.log(data);      
            },
            error: function(data) {
                console.log(data);
            }
        });
    }

It returns this response:

"{"error":{"code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"Access denied. You do not have permission to perform this action or access this resource."}}}"

I am referencing only two scripts:

 <script src="js/jquery-3.1.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" type="text/javascript"></script>

It says I am unauthorised to access. Although I am having admin rights to my list. Please tell me how to fix this issue. Do I need any authorization header for ajax call to Rest Api? I am new to Sharepoint. Please Help!

Sonali
  • 2,223
  • 6
  • 32
  • 69
  • Is it on premise or SharePoint Online? Maybe this can help you https://social.technet.microsoft.com/Forums/office/en-US/e9924aef-28df-43a4-8d79-a9d1532699c6/systemunauthorizedaccessexception-when-accessing-sharepoint-2013-rest-api?forum=sharepointsearch. Also this: http://sharepoint.stackexchange.com/questions/187926/system-unauthorizedaccessexception-when-accessing-the-sharepoint-2013-rest-api – R.Czq Oct 18 '16 at 09:42
  • Sharepoint Online – Sonali Oct 18 '16 at 09:52
  • i am creating this using js html only – Sonali Oct 18 '16 at 09:53
  • links are not helping – Sonali Oct 18 '16 at 09:54
  • It seems to be a permission problem, make sure that you have read/full control rights on the web as well, not only on the list. – R.Czq Oct 18 '16 at 09:59
  • 1
    What you mean by "creating this using js html only" where your JS code running... ? Are you hosting code in SharePoint itself? – vinayak hegde Oct 18 '16 at 12:35
  • No i am not hosting it in sharepoint it is separate – Sonali Oct 18 '16 at 15:33
  • If i am using on premise SP then how can I do it? – Sonali Oct 19 '16 at 10:10

3 Answers3

0

I think that custom scripting is disabled by default in SPOnline. You may have to enable it. These links may help:

Access denied office 365 / SharePoint online with Global Admin account

http://vamsi-sharepointhandbook.blogspot.com/2015/07/turn-scripting-capabilities-on-or-off.html

Community
  • 1
  • 1
RichG82
  • 21
  • 2
0

When you are trying to pull items from a list, specify the URL up to items. See below line for syntax -

$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('"+listName+"')/items",
// Rest of the code
});
nitinr708
  • 1,393
  • 2
  • 19
  • 29
0

Yes you would need a authorization header for your requests. So your headers should be like this:

headers: {
             "Accept": "application/json; odata=verbose",
             "Authorization": 'Bearer '+ **access_token**   
         }

access_token: You would need to acquire access token to make requests. You can use adal library provided by Microsoft for that.

viky.pat
  • 725
  • 9
  • 16