How to upload CSV file via graph api

When we use Microsoft Graph to upload files, according to the official document, we can only upload TXT document sample. So how do we upload other types of documents?

Firstly, the Microsoft Graph API does not support the following file uploads:

  • .ashx – ASP.NET Web handler file
  • .asmx – ASP.NET Web Services source file
  • .json – JavaScript Object Notation file
  • .soap – Simple Object Access Protocol file
  • .svc – Windows Communication Foundation (WCF) service file
  • .xamlx – Visual Studio Workflow service file

Except the above types of files , other types are supported by Microsoft Graph API, the following details will be given how to upload CSV / XlSX type of file:

URL: https://graph.microsoft.com/v1.0/me/drive/root:/FolderA/FileB.csv:/content

FolderAthe folder where you want to upload the file  FileB.csv: the name of the file

Method: Put

Permission:  Delegated     Files.ReadWrite, Files.ReadWrite.All, Sites.ReadWrite.All                                                                     Application   Files.ReadWrite.All, Sites.ReadWrite.All

Authentication:                                                                                                                                                      Post:/tenant-id/oauth2/token?api-version=1.0
Host:https://login.microsoftonline.com
Content-Type: application/x-www-form-urlencoded

resource=https://graph.microsoft.com&client_id=360f4211-331e-4260-9ae1-027327ccc81c&client_secret=6GXaE5M2Afg8gg2qZdI7TUoeqJjKQXENKKgOUDM4qys=&grant_type=password&username=**@**.onmicrosoft.com&password=***&scope=openid

Header: Content Type: application/vnd.ms-excel or text/csv

Body:
The Body section is especially noticeable here because CSV is in the form of a spreadsheet, so each cell in the Body should be separated by a comma, with the second line directly in the Body section if it is a newline. For example:
1a,11,7,fish                                                                                                                                                          2b,14,8,meat                                                                                                                                                           Result:                                                                                                                

Summary

In this article, I’ve demonstrated how to upload CSV file via graph API. Additionally, you can also upload the file types below as similar way. Chat with us  if you have any questions.

Suffix MIME Type
.doc application/msword
.dot application/msword
.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotx application/vnd.openxmlformats-officedocument.wordprocessingml.template
.docm application/vnd.ms-word.document.macroEnabled.12
.dotm application/vnd.ms-word.template.macroEnabled.12
.xls application/vnd.ms-excel
.xlt application/vnd.ms-excel
.xla application/vnd.ms-excel
.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltx application/vnd.openxmlformats-officedocument.spreadsheetml.template
.xlsm application/vnd.ms-excel.sheet.macroEnabled.12
.xltm application/vnd.ms-excel.template.macroEnabled.12
.xlam application/vnd.ms-excel.addin.macroEnabled.12
.xlsb application/vnd.ms-excel.sheet.binary.macroEnabled.12
.ppt application/vnd.ms-powerpoint
.pot application/vnd.ms-powerpoint
.pps application/vnd.ms-powerpoint

Leave a Reply

Your email address will not be published. Required fields are marked *