Outlook Create calendar event with NodeJS

5.2k Views Asked by At

I need to create a calendar event in outlook using NodeJS script. I have searched every where and tried different npm packages but i didn't got the solution.

I'm trying to create a calendar event using below code :

const rp = require('request-promise');

let token = "<access token>"

var jsonBody = {
    "Subject": "test event",
    "Body": {
        "ContentType": "HTML",
        "Content": "hello world"
    },
    "Start": {
        "DateTime": "2020-10-21T10:10:00",
        "TimeZone": "India Standard Time"
    },
    "End": {
        "DateTime": "2020-10-21T11:10:00",
        "TimeZone": "India Standard Time"
    },
    "location": {
        "displayName": "Noida"
    },
    "Attendees": [
        {
            emailAddress: {
                address: "[email protected]",
                name: "yyy yyy"
            },
            type: "required"
        },
        {
            emailAddress: {
                address: "[email protected]",
                name: "yyy yyy"
            },
            type: "required"
        }
    ]
};
///me/calendar/events
var optionsForCreatingcalendar = {
    uri: 'https://outlook.office.com/api/v2.0/me/events',
    port: 443,
    method: 'POST',
    headers: {
        'Authorization': 'Bearer ' + token,
        'Content-Type': 'application/json'
    },
    json: true,
    body: jsonBody,
    resolveWithFullResponse: true,
    simple: false
};

// --- API call using promise-----
rp(optionsForCreatingcalendar)
    .then(function (response) {
        console.log(response);
    }, function (err) {
        console.log(err);

    });

But there is some problem with code. it is throwing error like:

 error: {
      code: 'UnableToDeserializePostBody',
      message: 'were unable to deserialize '
    }

Please help me what i'm missing in the above code.

Thanks

3

There are 3 best solutions below

3
On BEST ANSWER

https://github.com/jasonjoh/node-outlook or https://www.npmjs.com/package/node-outlook

I would like to suggest you those libraries to use calendar event API. If you don't want to use it, then you need to send serialized data via outlook API.

Sample code to create an event.

var outlook = require('node-outlook');

var newEvent = {
    "Subject": "Discuss the Calendar REST API",
    "Body": {
        "ContentType": "HTML",
        "Content": "I think it will meet our requirements!"
    },
};

let createEventParameters = {
    token: ['access token will come here'],
    event: newEvent
};
outlook.calendar.createEvent(createEventParameters, function (error, event) {
    if(error) {
        console.log(error);                 
    } else {
        console.log(event);                         
    }
});

In your case, you need to use jsonBody instead of newEvent. then It will work.

4
On
var outlook = require('node-outlook');

var jsonBody = {
    "Subject": "test event",
    "Body": {
        "ContentType": "HTML",
        "Content": "hello world"
    },
    "Start": {
        "DateTime": "2020-10-21T10:10:00",
        "TimeZone": "India Standard Time"
    },
    "End": {
        "DateTime": "2020-10-21T11:10:00",
        "TimeZone": "India Standard Time"
    },
    "location": {
        "displayName": "Noida"
    },
    "Attendees": [
        {
            emailAddress: {
                address: "[email protected]",
                name: "yyy yyy"
            },
            type: "required"
        },
        {
            emailAddress: {
                address: "[email protected]",
                name: "yyy yyy"
            },
            type: "required"
        }
    ]
};

let createEventParameters = {
    token: ['access token will come here'],
    event: jsonBody
};
outlook.calendar.createEvent(createEventParameters, function (error, event) {
    if(error) {
        console.log(error);                 
    } else {
        console.log(event);                         
    }
});

This is sample code to use node-outlook library.

1
On

Please use PascalCase for all properties in jsonBody. That will work.

var jsonBody = {
 Subject: "test event",
 Body: {
   ContentType: "HTML",
   Content: "hello world",
 },
Start: {
   DateTime: "2020-10-21T10:10:00",
   TimeZone: "India Standard Time",
 },
End: {
   DateTime: "2020-10-21T11:10:00",
   TimeZone: "India Standard Time",
},
Location: {
  DisplayName: "Noida",
},
Attendees: [
{
    EmailAddress: {
       Address: "[email protected]",
      Name: "yyy yyy",
    },
    Type: "required",
},
{
    EmailAddress: {
      Address: "[email protected]",
      Name: "yyy yyy",
    },
    Type: "required",
  },
 ],
};