c# - 是否可以创建带有附件的日历事件?

标签 c# microsoft-graph-api microsoft-graph-sdks microsoft-graph-calendar

我想使用 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/

相关文章:

c# - 如何在 AForge 中找到矩形内的圆圈

c# - 字段和属性如何关联?

c# - 菜鸟事: How to collect data from multiple tables most deftly?

javascript - 在 C# 中调用 Javascript 函数

php - 在 PHP 中使用 Outlook Api 创建新事件(使用 POST 方法)

c# - Microsoft.Graph 作为附件转发

c# - 如何在 Azure 函数(客户端凭据流)中使用 Microsoft Graph SDK,而不使用 Microsoft.Graph.Auth

javascript - MS graph API 在接听电话时不发送 codeStatus

azure - 在 Graph api 上使用客户端凭证流会返回没有范围的 access_token

c# - Microsoft Graph API - DriveItem 上的权限不起作用