问题
I am trying to add attachment to calendar using Office 365 Starter Project for ASP.NET MVC https://github.com/OfficeDev/O365-ASPNETMVC-Start. As it requires event id I can't add using method AddCalendarEventAsync. So I'm trying to update event using below code:
var outlookServicesClient = await AuthenticationHelper.EnsureOutlookServicesClientCreatedAsync("Calendar");
var thisEventFetcher = outlookServicesClient.Me.Calendar.Events.GetById(selectedEventId);
IEvent eventToUpdate = await thisEventFetcher.ExecuteAsync();
But 'eventToUpdate' has not Attachment set property. Please could you check this and explain how to add attachment to calendar.
Thank you.
回答1:
I tested add the attachment to the event with outlookservicesclient, but can't find attachment even the event created successfully .It seems outlookservicesclient not includes the attachment data . If you use Fiddler , you couldn't find the attachment data in post request .
You could try to add attachment to event with 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"
}
Please click here for detail information .
回答2:
Yes @nan-yu you were right can't find attachment data in n fiddler. As per documentation https://msdn.microsoft.com/en-us/office/office365/api/calendar-rest-operations#Createattachments it requires eventid to save attachment. So I was using code in 'UpdateCalendarEventAsync' method. Just like how attachment works with emails, you save as draft and add attachment to that draft. Below code adds attachment but when I fetch event using eventid the attachment property has no data.
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();
}
回答3:
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);
来源:https://stackoverflow.com/questions/35937288/cant-add-attachment-to-calendar-event