创建全天活动失败

时间:2018-11-02 16:44:17

标签: typescript microsoft-graph spfx

我正在尝试创建一个全天活动:

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

日志:

enter image description here

当我包含“ isAllDay”属性时,它失败并显示400(错误请求)。

我排除了该属性,并且正在创建没有问题的事件。

有什么建议吗?


编辑:忘了提及,如果我将isAllDay设为false,则会创建该事件。

EDIT2 :这是通过SPFx项目中的MSGraphClient连接的。

1 个答案:

答案 0 :(得分:4)

在创建“全天”事件时,您startend时间应该仅指定 date ,而不是日期和时间(或更准确地说,是时间)应该是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);
        });
    });