2

Recently our on-premise sharepoint is moved to SP O365 .

We used to read the data from on-premise sharepoint site using REST service now we have to point the service to SP O365 site.

We are not getting the data in JSON response. Can you please guide how to read the sharepoint O365 site list data?

REST Service format - https://***.com/sites/dev/SPApps/_api/web/Lists/getbytitle('SharepointList')/

I am getting the error - System.UnauthorizedAccessException Access denied. You do not have permission to perform this action or access this resource.

I am using postman to test the REST service and chose basic authentication and for header chose Accept- application/json;odata:verbose

  • 1
    Is there any error coming up? If yes, can you post the error in your question. Also if possible add your code snippet. This would help to fix the issue quickly. – Ahmad Zia Sep 22 '17 at 5:57
4

In order to get data from SharePoint online you must provide the identity before that. If you open your browser and sign in for the site collection and try to copy paste the same URL in browser you'll definitely get the result instead of Access Denied error.

Now to your question, if you are consuming the REST service within SharePoint then you dont need to authenticate. But if you are consuming the SharePoint Online REST outside of the environment (for e.g. Using a .Net Web Site, or console application), then you first need to authenticate against it. There is a library available to do this job hassle free. The library is named as ADAL (Active Directory Authentication Library), which the version 1. A new library came named as MSAL (Microsoft Authentication Library), which points toward version 2 endpoints. The difference b/w them is ADAL can authenticate against Office Work & School account (i.e. Azure AD) only while MSAL can authenticate against Office Work & School account as well as Microsoft Personal Accounts like Hotmail, Outlook etc.

https://aka.ms/aaddev this URL will help you getting started with. Although it looks a bit complex first but try to understand the concepts and you are done. Dont worry about all the URL and endpoints. ADAL/MSAL will take care of them.

3

It won't work same as on-premise for O365, you will need user context for making call to API, hence the error. your REST service format is correct, you can login to O365 and paste the API URL in browser directly and see if it returns data.

To get it working in postman you will have to follow below

  1. Generate Security
  2. Token Generate Access Token
  3. Get Request Digest

Refer blog for detail steps: blog link

Long thing short: Link here

2

It should work if you call it client side while logged in as a user with enough rights to execute the query. I do it often from JS with the following function:

function executeSPRestQuery(url, query, complete, failure) {
var xhr = new XMLHttpRequest(); 
xhr.onreadystatechange = function() {       
    if (xhr.readyState == XMLHttpRequest.DONE) {
        if (xhr.status == 200) {                
            complete(xhr.response);
        } else {
            failure(xhr.response);
        }
    }
}   
xhr.open("GET", url + query, true);
xhr.setRequestHeader("Accept", "application/json;odata=verbose");
xhr.send(); 

}

That works well on SP365, we migrated completely. If you call it from an external app, you need to authenticate first as others have said.

Sample call:

executeSPRestQuery(
   "https://xxxx.sharepoint.com", "/_api/web/lists(guid'7777777-7777-7777-7777-777777777777')/items?$top=5000", myqueryresponsefunction, myerrorfunction);

Alternatively and working just as well on SP365 without additional authentication:

    var ctx = new SP.ClientContext("https://xxxx.sharepoint.com");   
    var oList = ctx.get_web().get_lists().getByTitle('MyListName');
1

Solve It. System.UnauthorizedAccessException: Access is denied.

Then try this code in REST service to get all items

"/_api/Web/Lists/GetByTitle('SpTutorial')/Items";

For more reference

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.