Access SharePoint Sites/files using JavaScript and MS Graph API

MOUSSAOUI Mohammed 40 Reputation points
2024-08-21T14:59:12.2533333+00:00

Hello everyone,

I created an application on SharePoint and gave it the necessary permissions to access a .csv file stored on my SharePoint.

With Postman, I can access the file using the access token I generated :

User's image

but with JavaScript, I get a error :

User's image

Here is my code:

Can you please help me solve the problem?

$(document).ready(function() {
  fetchCSV();
});

async function fetchCSV() {
  const siteUrl = "https://awex.sharepoint.com";
  const fileUrl = "/sites/Intranet/Documents%20partages/AdUserlast180days.csv";
  const accessToken = '..........'; 
  
  try {
    const response = await fetch(`${siteUrl}/_api/web/GetFileByServerRelativeUrl('${fileUrl}')/$value`, {
  method: 'GET',
  headers: {
    'Authorization': 'Bearer ' + accessToken,
    'Accept': 'text/csv',
  },
  mode: 'no-cors' 
});

    if (!response.ok) {
      throw new Error(`Erreur HTTP ! Statut : ${response.status}`);
    }

    const csvData = await response.text();
    console.log(csvData); 
    console.log('File loaded successfully');


  } catch (error) {
    console.error('Erreur lors de la récupération du fichier CSV :', error);
  }
}


Microsoft 365 and Office Development Office JavaScript API
Microsoft 365 and Office SharePoint Development
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Yanli Jiang - MSFT 31,596 Reputation points Microsoft External Staff
    2024-08-22T08:17:20.1733333+00:00

    Hi @MOUSSAOUI Mohammed ,

    Welcome to Q&A forum!

    According to the information you provided, the error message is customized by you and the specific reason cannot be determined. According to my research, the mode: 'no-cors' is likely causing issues, as it prevents the browser from accessing the response data for security reasons. Also, SharePoint APIs can sometimes be tricky due to CORS (Cross-Origin Resource Sharing) restrictions.

    Here's a revised approach to help you solve the problem:

    1. Remove mode: 'no-cors': This setting prevents you from accessing the response content.
    2. Use a Proxy: If CORS is causing issues, you might need to use a proxy server or set up CORS in your SharePoint. However, using a proxy is often not recommended due to security concerns.
    3. I found a similar post for your reference:

    https://sharepoint.stackexchange.com/questions/301732/how-to-retrieve-csv-json-file-from-sharepoint-document-directory-with-restful-ca

    https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest#working-with-files-by-using-rest

    Good day!


    If the answer is helpful, please click "Accept as Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. MOUSSAOUI Mohammed 40 Reputation points
    2024-08-27T09:41:25.9633333+00:00

    Hi @Yanli Jiang - MSFT

    I tried removing mode mode: 'no-cors' but it's still the same problem.


  3. MOUSSAOUI Mohammed 40 Reputation points
    2024-08-27T09:42:03.31+00:00

    Hi @Yanli Jiang - MSFT

    I tried removing mode mode: 'no-cors' but it's still the same problem.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.