May 09 2024 10:32 AM
Hello
Is there any way to work with teams approvals using Microsoft Graph API 1.0 and c#?
It looks like that thre is no api library support but
Can i use the http way? and whats the urls for that?
By example to create one request from the web application (back code)
Thank you
May 12 2024 10:49 PM - edited May 13 2024 01:37 AM
@Bruno Sanz Marino - Thanks for raising your query.
There is no specific libraries for team approvals, you can indeed interact with the API using HTTP requests directly.
To create a request from your web application's backend code, you would typically follow these steps:
Authentication: Obtain an access token using the OAuth 2.0 authentication flow. You'll need to register your application in the Azure portal to get the necessary credentials.
Construct HTTP Request: Formulate an HTTP request to interact with the Microsoft Graph API. In this case, you'll want to use the appropriate endpoint for team approvals.
Send the Request: Use C#'s HttpClient class or any similar library to send the HTTP request.
Handle Response: Process the response from the Microsoft Graph API.
Here's a basic example of how you might create a request to create an approval request in a Microsoft Teams team using the Microsoft Graph API:
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
public class Program
{
private static readonly HttpClient httpClient = new HttpClient();
private const string graphApiBaseUrl = "https://graph.microsoft.com/v1.0/";
public static async Task Main(string[] args)
{
string accessToken = ""; // Your access token obtained after authentication
// Construct the request payload
var approvalRequest = new
{
templateId = "<template_id>",
... // Other properties as required by the API
};
string requestUrl = $"{graphApiBaseUrl}teams/{teamId}/approvals"; // Replace teamId with your actual team ID
// Serialize the payload to JSON
string jsonPayload = JsonConvert.SerializeObject(approvalRequest);
// Create the HTTP request
var request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
request.Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
// Send the request and handle the response
HttpResponseMessage response = await httpClient.SendAsync(request);
if (response.IsSuccessStatusCode)
{
Console.WriteLine("Approval request created successfully!");
}
else
{
Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
}
}
}
Thanks,
Nivedipa
------------------------------------------------------------------------------------------
If the response is helpful, please click "**Mark as Best Response**" and like it. You can share your feedback via Microsoft Teams Developer Feedback link. Click here to escalate.