How to Call MS Graph One Drive API in C#

sinha_pradyut sinha_pradyut 1 Reputation point

Method REST Path
List permissions GET /drive/items/{item-id}/permissions
Get permission GET /drive/items/{item-id}/permissions/{id}
Create link POST /drive/items/{item-id}/createLink
Invite people POST /drive/items/{item-id}/invite
Update PATCH /drive/items/{item-id}/permissions/{id}
Delete DELETE /drive/items/{item-id}/permissions/{id}

How can i call these above MS GRAPH One drive API from C# code

Lets say I want to create a webservice in c# by calling the above APIs

Can anyone send some sample c# code

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,320 questions
A set of technologies in the .NET Framework for building web applications and XML web services.
3,404 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,790 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce ( 60,871 Reputation points

  2. CarlZhao-MSFT 39,746 Reputation points

    Hi @sinha_pradyut sinha_pradyut

    You can try calling these API endpoints using the C# graph SDK:

    using Azure.Identity;  
    using Microsoft.Graph;  
    using Newtonsoft.Json;  
    var scopes = new[] { "" };  
    var tenantId = "{tenant id}";  
    // Values from app registration  
    var clientId = "{client id}";  
    var clientSecret = "{client secret}";  
    // using Azure.Identity;  
    var options = new TokenCredentialOptions  
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud  
    var clientSecretCredential = new ClientSecretCredential(  
        tenantId, clientId, clientSecret, options);  
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);  
    //List permissions GET /drive/items/{item-id}/permissions  
    var permissions = await graphClient.Users["{user id}"].Drive.Items["{item id}"].Permissions  
    Console.WriteLine("permissions:" + JsonConvert.SerializeObject(permissions));  


    //Get permission GET /drive/items/{item-id}/permissions/{id}  
    var permission = await graphClient.Users["{user id}"].Drive.Items["{item id}"].Permissions["{permission id}"]  
    Console.WriteLine("permission:" + JsonConvert.SerializeObject(permission));  
    //Create link POST /drive/items/{item-id}/createLink  
    var type = "view";    
    var password = "ThisIsMyPrivatePassword";    
    var scope = "anonymous";      
    await graphClient.Users["{user id}"].Drive.Items["{driveItem-id}"]  
    //Invite people POST /drive/items/{item-id}/invite  
    var recipients = new List<DriveRecipient>()  
     new DriveRecipient  
     Email = ""  
    var message = "Here's the file that we're collaborating on.";  
    var requireSignIn = true;  
    var sendInvitation = true;  
    var roles = new List<String>()  
    var password = "password123";  
    var expirationDateTime = "2023-07-15T14:00:00Z";  
    await graphClient.Users["{user id}"].Drive.Items["{item id}"]  
    //Update PATCH /drive/items/{item-id}/permissions/{id}  
    var permission = new Permission  
     Roles = new List<String>()  
    await graphClient.Users["{user id}"].Drive.Items["{driveItem-id}"].Permissions["{permission-id}"]  
    //Delete DELETE /drive/items/{item-id}/permissions/{id}  
    await graphClient.Users["{user id}"].Drive.Items["{driveItem-id}"].Permissions["{permission-id}"]  

    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.

  3. Pradyut Sinha 21 Reputation points

    This code is not running. Unable to connect and different compilation error is given in the GraphClieent Parameter