Microsoft Graph - Proper way to retrieve attendance report from an Event

461 Views Asked by At

CONTEXT

Using application permission i'm creating an event and then retrieve the attendance report from it.

CODE TO ADD EVENT

  public static async Task<Event> CreateEventAsync(string emailUser, Event @event, string calendarId)
        {
            try
            {
                // Add /Events
                return await graphClient.Users[emailUser].Calendars[calendarId].Events
                    .Request()
                    .AddAsync(@event);
            }
            catch (Exception ex)
            {
                throw new MosException(ex.Message);
            }
        }

CODE TO GET ATTENDANCE REPORT

GraphHelper.Initialize();
var organizer = await GraphHelper.GetUserAsync(emailUser);
var reponseCalendar = await GraphHelper.GetCalendarAsync(emailUser);
var graphUserId = organizer.Id;

var linktoWebUrl = "/onlineMeetings?$filter=JoinWebUrl%20eq%20'https://teams.microsoft.com/l/meetup-join/19%3ameeting_YTEz.....etc...'";

var linkGraph = "https://graph.microsoft.com/v1.0/users/" + graphUserId + linktoWebUrl;

var accessToken = GraphHelper.GetToken().Result;
var httpClient = new HttpClient();
var apiCaller = new ProtectedApiCallHelper(httpClient);
var meetingObject = await apiCaller.CallWebApiAndGetASync(linkGraph, accessToken);

var meeetingIdJson = meetingObject.Last.First.First.First.Last;//<- not cool :(
var meeetingId = meeetingIdJson.ToString();

var newLink = "https://graph.microsoft.com/beta/users/" + graphUserId + "/onlineMeetings/" + meeetingId + "/attendanceReports ";
var meetingOnline = await apiCaller.CallWebApiAndGetASync(newLink, accessToken);

var attendanceReportsByUrl = meetingOnline.Last.First.First;//<- not cool :(

RESPONSE

Response

PROBLEME

Is there a way to do it in a proper way BUT, of course, from an Event and not an OnlineMeeting?

1

There are 1 best solutions below

2
On

Firstly, what you mentioned <- not cool can be solved when using graph client sdk like what you did in add event in my opinion. You can then avoid using code like .Last.First.

Then your ultimate goal is getting attendance report, so you have to using OnlineMeeting instead of Event because the api is provided by Online meeting.

And you may try code below, and I noticed that you used /beta version api, so you need to use the beta version SDK if you want to continue to use beta version:

enter image description here

using Microsoft.Graph;
using Azure.Identity;

public async Task<string> getReportAsync() {
    var scopes = new[] { "https://graph.microsoft.com/.default" };
    var tenantId = "your_tenant_name.onmicrosoft.com";
    var clientId = "azure_ad_clientid";
    var clientSecret = "client_secret";
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret);
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

    //isOnlineMeeting and onlineMeeting don't support filtering, so using Event is not a good choice
    //var events = await graphClient.Users["user_id"].Events
    //            .Request()
    //            .Select("isOnlineMeeting,onlineMeeting")
    //            .GetAsync();

    var meeting = await graphClient.Users["user_id"].OnlineMeetings
                    .Request()
                    .Filter("JoinWebUrl eq 'join_url'")
                    //.Select("id")
                    .GetAsync();
    var meetingId = meeting.FirstOrDefault().Id;

    var attendanceReports = await graphClient.Users["user_id"].OnlineMeetings[meetingId].AttendanceReports
                            .Request()
                            .GetAsync();
    return "success";
}