How to upload a .csv or .xlsx file to a SharePoint site using the Microsoft Graph Files API

Cyrus Majidy 25 Reputation points
2023-01-23T23:17:08.2366667+00:00

My company wants to move data in .xlsx files from a SharePoint channel to a database. A part of this process is being able to archive the files within the SharePoint site. I have been working tirelessly to figure out how to upload a .xlsx file to a SharePoint site/channel using the Microsoft Graph Files API, as it is described here in the Microsoft documentation:

[https://learn.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http

I have also consulted numerous other sources. However, the same problems always keep occurring. If I attempt to upload .xlsx files, the file is uploaded, but is corrupted and cannot be open!

ExcelIssue1

ExcelIssue2

ExcelIssue3

When trying .csv files, the file is uploaded, but when it is open, all of the rows of data are on one single long row!

My PowerShell code is formatted as follows:

#Upload
$Headers = @{
    "Content-Type"  = “text/plain”
    "Authorization" = "Bearer $Token"
}

$Filepath = “{path-to-file}“
$uri = "https://graph.microsoft.com/v1.0/sites/{site-id}/drive/items/{parent-item-id}:/2Sheets.xlsx:/content"

$Content = Get-Content -Path $Filepath

$MessageParams = @{
    "URI"         = $uri
    "Headers"     = $Headers
    "Method"      = "PUT"
    "Body"        = $Content
}
$Response = Invoke-RestMethod @Messageparams -TimeoutSec 15

I have followed the documentation to the letter, and tried everything I possibly could find or think of. There is not much documentation or solutions on the internet, and none that I have found to work. Is there something wrong with how I have formatted my PUT request, or is this a bug in the Microsoft Graph Files API or SharePoint Online?

Please let me know, thank you!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,449 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,302 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,332 questions
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 33,641 Reputation points Microsoft Vendor
    2023-01-25T06:42:05.7866667+00:00

    Hi @Cyrus Majidy

    I'm glad to hear you solve the problem ,if you have any issue about SharePoint, you are welcome to raise a ticket in this forum.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others." and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    How to upload a .csv or .xlsx file to a SharePoint site using the Microsoft Graph Files API

    Issue Symptom:

    Upload a .csv or .xlsx file to sharepoint site by graph. And will get error that file is corrupted and cannot be open

    Current status:

    Use [System.IO.File]::ReadAllBytes() upload file to the SharePoint site and keep the file intact

    $Content = [System.IO.File]::ReadAllBytes($Filepath)
    

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!


2 additional answers

Sort by: Most helpful
  1. Cyrus Majidy 25 Reputation points
    2023-01-24T19:41:53.1633333+00:00

    Thank you @RaytheonXie_MSFT for your answer, I appreciate your time.

    I actually found the answer on this page: https://powershell-guru.com/powershell-tip-135-read-file-as-a-byte-array/

    This PowerShell Cmdlet was not working, and producing corrupt .xlsx files and .csv files being collapsed to one line, when attempting to upload to a SharePoint site using Microsoft Graph Files API, even after adding "-Encoding Byte":

    $Content = Get-Content -Path $Filepath -Encoding Byte
    

    Instead, I used this, and the .xlsx files were uploaded to the SharePoint site, remaining fully intact:

    $Content = [System.IO.File]::ReadAllBytes($Filepath)
    
    1 person found this answer helpful.
    0 comments No comments

  2. RaytheonXie_MSFT 33,641 Reputation points Microsoft Vendor
    2023-01-24T09:04:17.1566667+00:00

    Hi @Cyrus Majidy

    Per my test, you can try following script to upload excel file.

    $text_data=file_get_contents('C:\Users\Downloads/testfile.xlsx');
    $graph = new Graph();
    $graph->setAccessToken($accessToken);
    $data = $graph->createRequest('PUT', '/sites/<siteid>/drives/b!163gxxxxxxxxxxxxxxxxxxxxxxxx/root:/testfile.xlsx:/content')
                    ->addHeaders(array('Content-Type' => 'text/plain'))
                    ->attachBody($text_data)
                    ->setReturnType(Model\User::class)
                    ->execute();
    
    

    If the answer is helpful, please click "Accept 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.