-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphHelper.cs
161 lines (140 loc) · 6.14 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using Azure.Identity;
using Microsoft.Graph;
using Newtonsoft.Json;
class GraphHelper
{
private static Settings? _settings;
private static ClientSecretCredential? _clientSecretCredential;
private static GraphServiceClient? _appClient;
public static void EnsureGraphForAppOnlyAuth(Settings settings)
{
_settings = settings;
_ = _settings ?? throw new System.NullReferenceException("Settings cannot be null");
if (_clientSecretCredential == null)
{
_clientSecretCredential = new ClientSecretCredential(_settings.TenantId, _settings.ClientId, _settings.ClientSecret);
}
if (_appClient == null)
{
_appClient = new GraphServiceClient(_clientSecretCredential, new[] { "https://graph.microsoft.com/.default" });
}
}
public static async Task SendReportMailAsync(string subject, string body, string attachmentPath)
{
_ = _appClient ?? throw new System.NullReferenceException("Graph has not been initialized for app-only auth");
_ = _settings ?? throw new System.NullReferenceException("Settings cannot be null");
IMessageAttachmentsCollectionPage attachments = new MessageAttachmentsCollectionPage()
{
new FileAttachment
{
Name = Path.GetFileName(attachmentPath),
ContentType = "text/plain",
ContentBytes = System.IO.File.ReadAllBytes(attachmentPath)
}
};
var message = new Message
{
Subject = subject,
Body = new ItemBody
{
Content = body,
ContentType = BodyType.Text
},
ToRecipients = new Recipient[]
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = _settings.ADUser,
}
}
},
Attachments = attachments
};
await _appClient.Users[_settings.ADUser]
.SendMail(message)
.Request()
.PostAsync();
}
public static async Task SendMailAsync(string recipient, string inetMsgID, string subject, string body)
{
_ = _appClient ?? throw new System.NullReferenceException("Graph has not been initialized for app-only auth");
_ = _settings ?? throw new System.NullReferenceException("Settings cannot be null");
var message = new Message
{
Subject = subject,
InternetMessageId = inetMsgID,
Body = new ItemBody
{
Content = body,
ContentType = BodyType.Html
},
ToRecipients = new Recipient[]
{
new Recipient
{
EmailAddress = new EmailAddress
{
Address = recipient.Trim(),
}
}
}
};
await _appClient.Users[_settings.ADUser]
.SendMail(message)
.Request()
.PostAsync();
}
public static async Task SendReplyAsync(string msgID, string comment)
{
_ = _appClient ?? throw new System.NullReferenceException("Graph has not been initialized for app-only auth");
_ = _settings ?? throw new System.NullReferenceException("Settings cannot be null");
await _appClient.Users[_settings.ADUser].Messages[msgID]
.ReplyAll(null, comment)
.Request()
.PostAsync();
}
public static Task<IMailFolderMessagesCollectionPage> GetSentItemAsync(string inetmsgid)
{
_ = _appClient ?? throw new System.NullReferenceException("Graph has not been initialized for app-only auth");
_ = _settings ?? throw new System.NullReferenceException("Settings cannot be null");
return _appClient.Users[_settings.ADUser].MailFolders["sentitems"].Messages
.Request()
.Select(m => new { m.Id })
.Filter("(internetMessageId eq '" + inetmsgid + "')")
.GetAsync();
}
public static Task<IWorkbookTableRowsCollectionPage> GetTableRowsAsync(string tableid)
{
_ = _appClient ?? throw new System.NullReferenceException("Graph has not been initialized for app-only auth");
_ = _settings ?? throw new System.NullReferenceException("Settings cannot be null");
return _appClient.Users[_settings.ADUser].Drive.Root.ItemWithPath(_settings.DocumentPath).Workbook.Tables[tableid].Rows
.Request()
.Select(r => new { r.Index, r.Values })
.GetAsync();
}
public static Task<WorkbookTableRow> AddTableRowAsync(string tableid, dynamic[][] values)
{
_ = _appClient ?? throw new System.NullReferenceException("Graph has not been initialized for app-only auth");
_ = _settings ?? throw new System.NullReferenceException("Settings cannot be null");
var workbookTableRow = new WorkbookTableRow
{
Values = System.Text.Json.JsonDocument.Parse(JsonConvert.SerializeObject(values, Formatting.Indented))
};
return _appClient.Users[_settings.ADUser].Drive.Root.ItemWithPath(_settings.DocumentPath).Workbook.Tables[tableid].Rows
.Request()
.Header("Prefer", "respond-async")
.AddAsync(workbookTableRow);
}
public static async Task<bool> DeleteTableRowAsync(string tableid, WorkbookTableRow row)
{
_ = _appClient ?? throw new System.NullReferenceException("Graph has not been initialized for app-only auth");
_ = _settings ?? throw new System.NullReferenceException("Settings cannot be null");
var requestUrl = _appClient.Users[_settings.ADUser].Drive.Root.ItemWithPath(_settings.DocumentPath).Workbook.Tables[tableid].Rows.ItemAt(row.Index.GetValueOrDefault()).Request().RequestUrl;
HttpRequestMessage hrm = new(HttpMethod.Delete, requestUrl);
await _appClient.AuthenticationProvider.AuthenticateRequestAsync(hrm);
HttpResponseMessage response = await _appClient.HttpProvider.SendAsync(hrm);
return response.IsSuccessStatusCode;
}
}