1

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 Answers 3

1

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.

3
  • I read about this NPM package but they don't mentioned like how to pass event time and attendees with this API? if you know about these thing please let me know
    – shiva
    Oct 20, 2020 at 8:07
  • In your case, you need to use jsonBody instead of newEvent. then It will work.
    – alpha-bird
    Oct 20, 2020 at 8:11
  • Can you please give me the sample JSON body to be post?
    – shiva
    Oct 20, 2020 at 8:14
1
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.

4
1

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",
  },
 ],
};
1
  • Holy crap. Took me ages to find this. Why do all of their HTTP examples in the documentation use camelCase?
    – PitaJ
    Aug 17, 2022 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.