问题
I'm trying to create an All Day event:
let foobar: any = {
"subject": calendarEvent.Title+"v5",
"body": {
"contentType": "HTML",
"content": calendarEvent! || !calendarEvent.Description ? "No Description": calendarEvent.Description,
},
"start": {
"dateTime": calendarEvent.EventDate,
"timeZone": moment.tz.guess(),
},
"end": {
"dateTime": calendarEvent.EndDate,
"timeZone": moment.tz.guess(),
},
"location": {
"displayName": !calendarEvent || !calendarEvent.Location ? "No Location": calendarEvent.Location,
},
"isAllDay": !calendarEvent || !calendarEvent.fAllDayEvent ? false : true,
};
context.msGraphClientFactory.getClient()
.then((client: MSGraphClient) => {
client.api("/me/calendar/events").post(foobar)
.then((content: any) => {
console.log("CalendarService | createCalendarEvent | content: ", content);
});
});
Log:
When I include the `isAllDay" property, it fails with a 400 (Bad Request).
I exclude the property, and it's creating the event w/out issue.
Any suggestions?
EDIT: forgot to mention, if I pass isAllDay
as false
, the event is created.
EDIT2: This is connecting through the MSGraphClient from an SPFx project.
回答1:
When creating an "All Day" event, you start
and end
times should only specify the date, not the date and time (or more accurately, the time should be 00:00:00):
let foobar: any = {
"subject": calendarEvent.Title+"v5",
"body": {
"contentType": "HTML",
"content": calendarEvent! || !calendarEvent.Description ? "No Description": calendarEvent.Description,
},
"start": {
"dateTime": !calendarEvent.fAllDayEvent ? calendarEvent.EventDate : calendarEvent.EventDate.setTime(0),
"timeZone": moment.tz.guess(),
},
"end": {
"dateTime": !calendarEvent.fAllDayEvent ? calendarEvent.EventDate : calendarEvent.EventDate.setTime(0),
"timeZone": moment.tz.guess(),
},
"location": {
"displayName": !calendarEvent || !calendarEvent.Location ? "No Location": calendarEvent.Location,
},
"isAllDay": !calendarEvent || !calendarEvent.fAllDayEvent ? false : true,
};
context.msGraphClientFactory.getClient()
.then((client: MSGraphClient) => {
client.api("/me/calendar/events").post(foobar)
.then((content: any) => {
console.log("CalendarService | createCalendarEvent | content: ", content);
});
});
来源:https://stackoverflow.com/questions/53122667/creating-all-day-event-failing