How to Call MS Graph One Drive API in C#

sinha_pradyut sinha_pradyut 1 Reputation point
2022-11-28T17:47:31.06+00:00

Methods
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.
10,292 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,219 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,615 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 53,426 Reputation points
    2022-11-28T19:03:54.617+00:00

  2. CarlZhao-MSFT 36,001 Reputation points
    2022-11-29T08:04:39.263+00:00

    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[] { "https://graph.microsoft.com/.default" };  
      
    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  
    };  
      
    // https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential  
    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  
     .Request()  
     .GetAsync();      
    Console.WriteLine("permissions:" + JsonConvert.SerializeObject(permissions));  
    

    265087-image.png

    //Get permission GET /drive/items/{item-id}/permissions/{id}  
    var permission = await graphClient.Users["{user id}"].Drive.Items["{item id}"].Permissions["{permission id}"]  
     .Request()  
     .GetAsync();      
    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}"]  
     .CreateLink(type,scope,null,password,null,null)  
     .Request()  
     .PostAsync();  
    
    //Invite people POST /drive/items/{item-id}/invite  
    var recipients = new List<DriveRecipient>()  
    {  
     new DriveRecipient  
     {  
     Email = "ryan@contoso.com"  
     }  
    };  
      
    var message = "Here's the file that we're collaborating on.";  
      
    var requireSignIn = true;  
      
    var sendInvitation = true;  
      
    var roles = new List<String>()  
    {  
     "write"  
    };  
      
    var password = "password123";  
      
    var expirationDateTime = "2023-07-15T14:00:00Z";  
      
    await graphClient.Users["{user id}"].Drive.Items["{item id}"]  
     .Invite(recipients,requireSignIn,roles,sendInvitation,message,null,expirationDateTime,password)  
     .Request()  
     .PostAsync();  
    
    //Update PATCH /drive/items/{item-id}/permissions/{id}  
    var permission = new Permission  
    {  
     Roles = new List<String>()  
     {  
     "read"  
     }  
    };  
      
    await graphClient.Users["{user id}"].Drive.Items["{driveItem-id}"].Permissions["{permission-id}"]  
     .Request()  
     .UpdateAsync(permission);  
    
    //Delete DELETE /drive/items/{item-id}/permissions/{id}  
    await graphClient.Users["{user id}"].Drive.Items["{driveItem-id}"].Permissions["{permission-id}"]  
     .Request()  
     .DeleteAsync();  
    

    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
    2022-12-18T12:01:30.2+00:00

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