无法在日历活动

时间:2016-03-11 10:07:39

标签: office365 office365api

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

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

但是' eventToUpdate'没有附件集属性。请您检查一下,并解释如何在日历中添加附件。

谢谢。

3 个答案:

答案 0 :(得分:0)

我测试了使用outlookservicesclient向事件添加附件,但即使事件创建成功也无法找到附件。似乎outlookservicesclient不包含附件数据。如果您使用Fiddler,则无法在帖子请求中找到附件数据。

您可以尝试使用Graph 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了解详细信息。

答案 1 :(得分:0)

是的@ 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();
            }

答案 2 :(得分:0)

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);