Best way to download multiple files from Sharepoint using MSFT Graph API

User3 40 Reputation points
2024-06-15T14:45:45.06+00:00

Hi I am using MSFT Graph API to download files from Sharepoint. I have already retrieved the fileIds of the files using the delta request so the question is just about file downloads.

  1. When I make a request to https://graph.microsoft.com/v1.0/sites/$siteId/drives/$driveId/items/$fileId/content
    I get redirected to a download URL and there I get the content. So I'm making 2 requests for every file. Is there a better way to do this? And do both the requests count towards API quota?
  2. Does using batch mode help? Because here if I use batch mode and send a request for 10 files, I get 10 download URLs in the output, not the content of the files. I still have to make 10 individual requests to those URLs. And those URLS appear to be hosted on my Sharepoint domain so they will still count towards the API quota?
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,418 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,270 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 33,641 Reputation points Microsoft Vendor
    2024-06-17T02:01:02.2766667+00:00

    Hi @User3,

    You can use JSON batching option to download up to 20 files from OneDrive/SharePoint Online with Graph API. JSON batching allows you to optimize your application by combining multiple requests (up to 20) into a single JSON object. Please find the couple of examples below on the same :

    POST https://graph.microsoft.com/v1.0/$batch  
    {  
        "requests": [  
            {  
                "id": "1",  
                "method": "GET",  
                "url": "/me/drive/root:/test.docx:/content"  
            },  
            {  
                "id": "2",  
                "method": "GET",  
                "url": "/me/drive/root:/testPDF.pdf:/content"  
            },  
            {  
                "id": "3",  
                "method": "GET",  
                "url": "/me/drive/root:/testPPT.pptx:/content"  
            },  
            {  
                "id": "4",  
                "method": "GET",  
                "url": "/me/drive/root:/testExcel.xlsx:/content"  
            }  
        ]  
    }
    
    
    

    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.