How to download file fromsharepoint using api?

Ritvik Johar 21 Reputation points
2022-11-08T17:07:49.797+00:00

Hi,

I am trying to download files from sharepoint, I have been able to get to the desired folder using https://graph.microsoft.com/v1.0/sites/{siteid}/lists/{listid}/items/{item-id}/ but cannot view the files.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,651 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,675 questions
{count} votes

Accepted answer
  1. Srinivasa Rao Darna 6,691 Reputation points Microsoft Vendor
    2022-11-09T08:11:11.1+00:00

    Hello @Ritvik Johar ,

    If you are looking to download file uploaded in document library referred as driveItem driveitem-get-content.

    GET /sites/{site-id}/drives/{drive-id}/root:/{path-relative-to-root}:/content  
    GET /sites/{site-id}/drives/{drive-id}/items/{item-id}/content  
    

    If you are looking to download file uploaded to a list item referred itemAttachment, currently Microsoft Graph doesn't have support for list item attachments, you can upvote feature requested submitted by other Graph users.
    Add support for SharePoint List Item Attachments in Graph
    Ability to attach files to list items using Graph API.

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

    2 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Rob Windsor 1,956 Reputation points
    2022-11-09T07:14:47.023+00:00

    Here's a link to the documentation on downloading files using Microsoft Graph

    Download the contents of a DriveItem

    Also, these Microsoft Learn training modules cover how to download files using Microsoft Graph and C# or JavaScript.

    Access Files with Microsoft Graph (using C#)

    Download and upload files in a JavaScript app with Microsoft Graph

    The examples in the Microsoft Learn training modules work with files in OneDrive instead of SharePoint. There are a few ways to build a URL to get a SharePoint file in Microsoft Graph, I'll show you one of them.

    Let's say you have files in a library at https://m365x214355.sharepoint.com/sites/contoso/Documents, you can get the items in that library using Microsoft Graph by making a GET request to https://graph.microsoft.com/v1.0/sites/M365x214355.sharepoint.com:/sites/contoso:/lists/Documents/items. The general format of that request is https://graph.microsoft.com/v1.0/sites/{hostname}:{server-relative-path}:/lists/{list-name}/items.

    You can then get the specific file by appending its list item Id. For example, https://graph.microsoft.com/v1.0/sites/M365x214355.sharepoint.com:/sites/contoso:/lists/Documents/items/2.

    258566-image.png

    This gives you a List Item resource, but you need a Drive Item resource to be able to download the file. Fortunately, you can easily get the Drive Item by appending /driveitem to the URL. For example, https://graph.microsoft.com/v1.0/sites/M365x214355.sharepoint.com:/sites/contoso:/lists/Documents/items/2/driveitem.

    258623-image.png

    Finally, you can get the content for the file by appending /content to the URL. For example, https://graph.microsoft.com/v1.0/sites/M365x214355.sharepoint.com:/sites/contoso:/lists/Documents/items/2/driveitem/content

    Hopefully you'll be able to combine this technique to address the Drive Item resource for a file in SharePoint and the examples in the Microsoft Learn training modules to be able to download files.

    4 people found this answer helpful.

  2. RaytheonXie_MSFT 31,606 Reputation points Microsoft Vendor
    2022-11-09T06:31:02.23+00:00

    Hi @Ritvik Johar
    You can use following api to download file.

     GET /drives/{drive-id}/items/{item-id}/content  
    

    I have found a similar issue for you to reference
    https://learn.microsoft.com/en-us/answers/questions/601256/downloading-docuemtn-form-sharepoint-documents-usi.html


    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.


    1 person found this answer helpful.