Download and Upload files via Graph API from SharePoint lists and document libraries. C# .NET6

Amit Singh Rawat 731 Reputation points
2023-08-17T06:12:20.09+00:00

Hi,

I want to download files from my SharePoint Site (Not One Drive). And with the help of GraphServiceClient, I am able to get SiteID, ListID and also itemId.

Now how can I upload and download files to this library with the help of Graph API (GraphServiceClient).

This is how I am creating object of GraphServiceClient;

ClientSecretCredential clientSecretCredential = new(tenantID, clientID, clientSecret);
                GraphServiceClient graphClient = new(clientSecretCredential);
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,465 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,923 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,094 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.
3,107 questions
{count} votes

1 answer

Sort by: Most helpful
  1. ChengFeng - MSFT 5,040 Reputation points Microsoft Vendor
    2023-08-18T06:27:02.2833333+00:00

    Hi @ASR

    It is recommended to browse the official documentation here, which provides code examples for your reference:

    Here is Download file:

    // Code snippets are only available for the latest version. Current version is 5.x
    var graphClient = new GraphServiceClient(requestAdapter);
    await graphClient.Drives["{drive-id}"].Items["{driveItem-id}"].Content.GetAsync();
    

    User's image

    Here is upload file:

    https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0

    https://stackoverflow.com/questions/41285403/upload-file-to-sharepoint-drive-using-microsoft-graph


    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.

    Best Regards

    Cheng Feng

    2 people found this answer helpful.

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.