Creating All Day Event failing

帅比萌擦擦* 提交于 2019-12-11 03:15:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!