Downloading multiple files from OneDrive using the Graph API

Ben Gallienne 6 Reputation points
2022-03-21T16:35:27.353+00:00

Hi,

We are using the Graph API to retrieve and manage files in a Sharepoint instance from within our platform. Currently all is working well, we can upload files, create folders, and download individual files again.

What we would now like to do is create a similar process found inside the OneDrive / SharePoint UI, where you can select multiple files at once, then click download and receive a zip file of the selected items.

From looking at the Graph API documentation, this method does not seem to exist, but looking at the OneDrive / SharePoint UI and what it does when the button is clicked, it appears to make a POST request to the following URL 'https://ukwest1-mediap.svc.ms/transform/zip?cs=fFNQTw' (the querystring value varies), and a payload containing a list of items required for download.

So I am asking, are we able to utilise the above URL, or is there a method in the Graph API that is undocumented that creates a zip file from multiple selections?

Any help that can be provided would be much appreciated!

Thanks,

Ben

Community Center Not monitored
{count} votes

1 answer

Sort by: Most helpful
  1. JanardhanaVedham-MSFT 3,566 Reputation points
    2022-03-21T18:38:02.647+00:00

    Hi @Ben Gallienne ,

    Download file Graph API allow us to download one file at a time from OneDrive/SharePoint Online and currently there is no Graph API availble to download multple selected files into zip file as we can do it in OneDrive/SharePoint Online UI. Even you will not be able to utilise above mentioned url to achive the same.

    However as a workaround, you can be able to 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 :

    Example 1:

    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"  
            }  
        ]  
    }  
    

    Example Output Screenshot from Graph Explorer :
    185371-image.png

    Example 2:

    POST https://graph.microsoft.com/v1.0/$batch  
    {  
        "requests": [  
            {  
                "id": "1",  
                "method": "GET",  
                "url": "/me/drives/b!wNpJe7WRRkC8Q5N2f-eDhDCULks5oblAhvAbCdsulXiHXcgsFNblR44xoX1r-ZkU/items/01H2CECZV4KXYRSVB3TZHIZGPQSBNNXP7Y/content"  
            },  
            {  
                "id": "2",  
                "method": "GET",  
                "url": "/me/drives/b!wNpJe7WRRkC8Q5N2f-eDhDCULks5oblEFGHbMcsulXiHXcgsFNblR44xoX1r-ZkU/items/01H2CECZWTHNXW3OYLLNDIWAFTEAAGWL4N/content"  
            },  
            {  
                "id": "3",  
                "method": "GET",  
                "url": "/me/drives/b!wNpJe7WRRkC8Q5N2f-eDhDCULks5ijkAhvRbMcsulXiHXcgsFNblR44xoX1r-ZkU/items/01H2CECZUNFJEYLB3Y4ZGZB36TCYVLHATU/content"  
            }  
        ]  
    }  
    

    Example Output Screenshot from Graph Explorer :
    185381-image.png

    Hope this helps.

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


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.