无法将附件添加到日历事件

can't add attachment to calendar event

我正在尝试使用 ASP.NET MVC https://github.com/OfficeDev/O365-ASPNETMVC-Start 的 Office 365 入门项目向日历添加附件。因为它需要事件 ID,所以我无法使用方法 AddCalendarEventAsync 添加。所以我正在尝试使用以下代码更新事件:

var outlookServicesClient = await AuthenticationHelper.EnsureOutlookServicesClientCreatedAsync("Calendar");
var thisEventFetcher = outlookServicesClient.Me.Calendar.Events.GetById(selectedEventId);
IEvent eventToUpdate = await thisEventFetcher.ExecuteAsync();

但是'eventToUpdate'还没有设置附件属性。请您检查一下并说明如何将附件添加到日历。

谢谢。

我测试了用 outlookservicesclient 将附件添加到事件中,但即使事件创建成功也找不到附件。看来 outlookservicesclient 不包括附件数据。如果您使用 Fiddler ,则无法在 post 请求中找到附件数据。

您可以尝试使用图表向事件添加附件 API :

POST https://graph.microsoft.com/v1.0/me/messages/<id>/attachments
Content-type: application/json
Content-length: 142

{
   "@odata.type": "#microsoft.graph.fileAttachment",
   "name": "name-value",
   "contentBytes": "contentBytes-value"
}

请点击here查看详细信息。

是的@nan-yu 你是对的,在 n fiddler 中找不到附件数据。根据文档 https://msdn.microsoft.com/en-us/office/office365/api/calendar-rest-operations#Createattachments,它需要 eventid 来保存附件。所以我在 'UpdateCalendarEventAsync' 方法中使用了代码。就像附件如何处理电子邮件一样,您可以另存为草稿并将附件添加到该草稿。下面的代码添加了附件,但是当我使用 eventid 获取事件时,附件 属性 没有数据。

Event newEvent = new Event
            {
             Subject = Subject,
                Location = location,
                Attendees = attendees,
                Start = start,
                End = end,
                Body = body
            };

            newEvent.Start = (DateTimeOffset?)CalcNewTime(newEvent.Start, start);
            newEvent.End = (DateTimeOffset?)CalcNewTime(newEvent.End, end);

            try
            {
                // Make sure we have a reference to the Outlook Services client
                var outlookServicesClient = await AuthenticationHelper.EnsureOutlookServicesClientCreatedAsync("Calendar");

                // This results in a call to the service.
                await outlookServicesClient.Me.Events.AddEventAsync(newEvent);
                newEventId = newEvent.Id;
                newEvent.Attachments = attachments;
                newEvent.HasAttachments = true;
                await ((IEventFetcher)newEvent).ExecuteAsync();
                await newEvent.UpdateAsync(true);
                var thisEventFetcher = outlookServicesClient.Me.Calendar.Events.GetById(newEventId);
                IEvent eventToUpdate = await thisEventFetcher.ExecuteAsync();
                await outlookServicesClient.Context.SaveChangesAsync();
            }
var thisEventFetcher = outlookServicesClient.Me.Calendar.Events.GetById(eventID);
Attachment attachment = new FileAttachment
                                {
                                    Name = "test",
                                    ContentBytes = File.ReadAllBytes(@"D:\test.jpeg"),
                                    ContentType = "image/jpeg"
                                };
                                await thisEventFetcher.Attachments.AddAttachmentAsync(attachment);