我想使用 Microsoft Graph 创建日历事件,这很有效,但遗憾的是,我无法向该事件添加附件。事件已创建,但没有附件。没有报错。
这是我的代码:
DateTimeTimeZone start = new DateTimeTimeZone
{
TimeZone = TimeZoneInfo.Local.Id,
DateTime = dateTimePicker1.Value.ToString("o"),
};
DateTimeTimeZone end = new DateTimeTimeZone
{
TimeZone = TimeZoneInfo.Local.Id,
DateTime = dateTimePicker2.Value.ToString("o"),
};
Location location = new Location
{
DisplayName = "Thuis",
};
byte[] contentBytes = System.IO.File
.ReadAllBytes(@"C:\test\sample.pdf");
var ev = new Event();
FileAttachment fa = new FileAttachment
{
ODataType = "#microsoft.graph.fileAttachment",
ContentBytes = contentBytes,
ContentType = "application/pdf",
Name = "sample.pdf",
IsInline = false,
Size = contentBytes.Length
};
ev.Attachments = new EventAttachmentsCollectionPage();
ev.Attachments.Add(fa);
ev.Start = start;
ev.End = end;
ev.IsAllDay = false;
ev.Location = location;
ev.Subject = textBox2.Text;
var response = await graphServiceClient
.Users["<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f287819780b2969d919c9796dc9c9e" rel="noreferrer noopener nofollow">[email protected]</a>"]
.Calendar
.Events
.Request()
.AddAsync(ev);
最佳答案
似乎仍然不支持在单个请求中创建带有附件的事件 ( a similar issue )
作为解决方法,可以先创建一个没有附件的事件,然后将附件添加到其中(需要向服务器发出两个请求),例如:
var ev = new Event
{
Start = start,
End = end,
IsAllDay = false,
Location = location,
Subject = subject
};
//1.create an event first
var evResp = await graphServiceClient.Users[userId].Calendar.Events.Request().AddAsync(ev);
byte[] contentBytes = System.IO.File.ReadAllBytes(localPath);
var attachmentName = System.IO.Path.GetFileName(localPath);
var fa = new FileAttachment
{
ODataType = "#microsoft.graph.fileAttachment",
ContentBytes = contentBytes,
ContentType = MimeMapping.GetMimeMapping(attachmentName),
Name = attachmentName,
IsInline = false
};
//2. add attachments to event
var faResp = await graphServiceClient.Users[userId].Calendar.Events[evResp.Id].Attachments.Request().AddAsync(fa);
关于c# - 是否可以创建带有附件的日历事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55075870/