-
Notifications
You must be signed in to change notification settings - Fork 1
/
GraphHelper.cs
86 lines (84 loc) · 2.89 KB
/
GraphHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using Microsoft.Graph;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace GraphMessagesSample
{
public class GraphHelper
{
private static GraphServiceClient graphClient;
public static void Initialize(IAuthenticationProvider authProvider)
{
graphClient = new GraphServiceClient(authProvider);
}
public static async Task<IEnumerable<Group>> GetGroupsAsync()
{
try
{
List<Group> groups = new List<Group>();
var resultPage = await graphClient.Groups.Request().GetAsync();
while (true)
{
groups.AddRange(resultPage.CurrentPage);
if (resultPage.NextPageRequest == null)
{
break;
}
resultPage = resultPage.NextPageRequest.GetAsync().Result;
}
return groups;
}
catch (ServiceException ex)
{
Console.WriteLine($"Error getting events: {ex.Message}");
return null;
}
}
public static async Task<IEnumerable<DirectoryObject>> GetGroupMembersAsync(string groupId)
{
try
{
List<DirectoryObject> dirobjects = new List<DirectoryObject>();
var resultPage = await graphClient.Groups[groupId].Members.Request().GetAsync();
while (true)
{
dirobjects.AddRange(resultPage.CurrentPage);
if (resultPage.NextPageRequest == null)
{
break;
}
resultPage = resultPage.NextPageRequest.GetAsync().Result;
}
return dirobjects;
}
catch (ServiceException ex)
{
Console.WriteLine($"Error getting events: {ex.Message}");
return null;
}
}
public static async Task<IEnumerable<Message>> GetMessagesAsync(string userId)
{
try
{
List<Message> dirobjects = new List<Message>();
var resultPage = await graphClient.Users[userId].Messages.Request().GetAsync();
while (true)
{
dirobjects.AddRange(resultPage.CurrentPage);
if (resultPage.NextPageRequest == null)
{
break;
}
resultPage = resultPage.NextPageRequest.GetAsync().Result;
}
return dirobjects;
}
catch (ServiceException ex)
{
Console.WriteLine($"Error getting events: {ex.Message}");
return null;
}
}
}
}