Introduction

In this article, I explain how to retrieve the items & files from SharePoint using Microsoft Graph API.
Why use Graph API?
Recently, I had a query from one developer. He needs to show the file information from a document library. It's always possible using SharePoint REST API, but they are facing a problem for certain users who not have access to the library.
Let’s achieve this using MS GraphAPI.
Endpoint: https://graph.microsoft.com/v1.0/sites/{siteid}/lists/{listid}/items
Permission Scope: Sites.Read.All
Follow the below article to learn how to fetch an access token using Microsoft Graph API:
In your tenant Azure portal, navigate to Azure active directory “App Registration”. Open your app to provide the permission for accessing the SharePoint site lists & libraries via Microsoft Graph API.
Once you navigate to the registered application -> Click API Permissions
Now Choose Add Permission -> Select Microsoft Graph API ->Application Permission -> Under “Sites” node select “Sites.Read.All”
Finally, it looks like below:
Once done, let’s display file information in SharePoint Content Editor Webpart with the help of some HTML/JQuery.
Create the function name “requestToken()” to fetch the access token on a page load:
  1. function requestToken() {
  2. $.ajax({
  3. "async": true,
  4. "crossDomain": true,
  5. "url": "https://howling-crypt-47129.herokuapp.com/https://login.microsoftonline.com/sharepointtechie.onmicrosoft.com/oauth2/v2.0/token", // Pass your tenant name instead of sharepointtechie
  6. "method": "POST",
  7. "headers": {
  8. "content-type": "application/x-www-form-urlencoded"
  9. },
  10. "data": {
  11. "grant_type": "client_credentials",
  12. "client_id ": "8baf0301-27df-44b1-b4fe-7911b9a918de", //Provide your app id
  13. "client_secret": "tZ76oVPN039WlWPoAp+1aICq66vs7oUtE4lhDQYwxGY=", //Provide your secret
  14. "scope ": "https://graph.microsoft.com/.default"
  15. },
  16. success: function(response) {
  17. console.log(response);
  18. token = response.access_token;
  19. console.log(token);
  20. FetchSharepointLibraries();
  21. },
  22. error: function(error) {
  23. console.log(JSON.stringify(error));
  24. }
  25. })
  26. }
Required Parameters
Client ID, Client Secret,Scope, Grant_Type: client_credentials By default
After successfully retrieving the access token, let’s create another function to query the SharePoint site lists/libraries
  1. function FetchSharepointLibraries(){
  2. var SPSiteID = "sharepointtechie.sharepoint.com,1a634409-8e14-4d07-b1a7-366eec870233,4047d743-691c-4578-9795-4c1458096e98"; // Sharepoint Site ID
  3. var ListID = "07f64111-6d12-47d0-9672-7124bdddce3b"; // List/LibraryID
  4. var GraphURL = "https://graph.microsoft.com/v1.0/sites/"+ SPSiteID +"/lists/"+ ListID +"/items"; //Constructed URL
  5. $.ajax({
  6. method: 'GET',
  7. url: GraphURL,
  8. headers: {
  9. 'Authorization': 'Bearer ' + token,
  10. 'Content-Type': 'application/json'
  11. },
  12. success: function(response) {
  13. var data = response.value;
  14. console.log(data);
  15. },error: function(error) {}
  16. })
  17. }
Let’s do some fancy work to display the files:
Full Code
  1. <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
  2. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  3. <script type="text/javascript">
  4. var token;
  5. $(document).ready(function () {
  6. requestToken();
  7. });
  8. function requestToken() {
  9. $.ajax({
  10. "async": true,
  11. "crossDomain": true,
  12. "url": "https://howling-crypt-47129.herokuapp.com/https://login.microsoftonline.com/sharepointtechie.onmicrosoft.com/oauth2/v2.0/token", // Pass your tenant name instead of sharepointtechie
  13. "method": "POST",
  14. "headers": {
  15. "content-type": "application/x-www-form-urlencoded"
  16. },
  17. "data": {
  18. "grant_type": "client_credentials",
  19. "client_id ": "8baf0301-27df-44b1-b4fe-7911b9a918de", //Provide your app id
  20. "client_secret": "tZ76oVPN039WlWPoAp+1aICq66vs7oUtE4lhDQYwxGY=", //Provide your secret
  21. "scope ": "https://graph.microsoft.com/.default"
  22. },
  23. success: function (response) {
  24. console.log(response);
  25. token = response.access_token;
  26. console.log(token);
  27. FetchSharepointLibraries();
  28. },
  29. error: function (error) {
  30. console.log(JSON.stringify(error));
  31. }
  32. })
  33. }
  34. function FetchSharepointLibraries() {
  35. var SPSiteID = "sharepointtechie.sharepoint.com,1a634409-8e14-4d07-b1a7-366eec870233,4047d743-691c-4578-9795-4c1458096e98";
  36. var ListID = "07f64111-6d12-47d0-9672-7124bdddce3b";
  37. var GraphURL = "https://graph.microsoft.com/v1.0/sites/" + SPSiteID + "/lists/" + ListID + "/items";
  38. $.ajax({
  39. method: 'GET',
  40. url: GraphURL,
  41. headers: {
  42. 'Authorization': 'Bearer ' + token,
  43. 'Content-Type': 'application/json'
  44. },
  45. success: function (response) {
  46. var data = response.value;
  47. var html = '';
  48. $.each(data, function (index, data) {
  49. var fileName = data.webUrl.split("Shared%20Documents/")[1];
  50. fileName = decodeURIComponent(fileName)
  51. html += "<li><a href="+ data.webUrl +">"+ fileName +"</a></li><br>";
  52. })
  53. $('#files').append(html)
  54. }, error: function (error) { }
  55. })
  56. }
  57. </script>
  58. <body>
  59. <ul id="files"></ul>
  60. </body>
Final output look like below:
keep Sharing …..