This code is not running. Unable to connect and different compilation error is given in the GraphClieent Parameter
How to Call MS Graph One Drive API in C#
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
3 answers
Sort by: Newest
-
-
CarlZhao-MSFT 36,976 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));
//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. -
Bruce (SqlWork.com) 56,026 Reputation points
2022-11-28T19:03:54.617+00:00 you should start with the docs:
https://learn.microsoft.com/en-us/onedrive/developer/sample-code?view=odsp-graph-online