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: "yyy@yyy.com",
                name: "yyy yyy"
            },
            type: "required"
        },
        {
            emailAddress: {
                address: "yyy@yyy.com",
                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

shiva
  • 429
  • 1
  • 6
  • 18

3 Answers3

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.

alpha-bird
  • 318
  • 2
  • 10
  • 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 '20 at 08:07
  • In your case, you need to use jsonBody instead of newEvent. then It will work. – alpha-bird Oct 20 '20 at 08:11
  • Can you please give me the sample JSON body to be post? – shiva Oct 20 '20 at 08: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: "yyy@yyy.com",
                name: "yyy yyy"
            },
            type: "required"
        },
        {
            emailAddress: {
                address: "yyy@yyy.com",
                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.

alpha-bird
  • 318
  • 2
  • 10
  • It gives me output like : { '@odata.context': 'https://outlook.office.com/api/v1.0/$metadata#Me/Events', value: [] } – shiva Oct 20 '20 at 08:34
  • 1
    You can refer to this link. https://github.com/jasonjoh/node-outlook/blob/master/reference/node-outlook.md#module_calendar.createEvent – alpha-bird Oct 20 '20 at 08:44
  • @shiva, Can you kindly click accept button for this answer? – alpha-bird Nov 01 '20 at 13:27
  • how to get token using email and password without opening any url on the browser in NodeJS? any idea of that? – shiva Nov 02 '20 at 11:12
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: "yyy@yyy.com",
      Name: "yyy yyy",
    },
    Type: "required",
},
{
    EmailAddress: {
      Address: "yyy@yyy.com",
      Name: "yyy yyy",
    },
    Type: "required",
  },
 ],
};
SimoMatavulj
  • 594
  • 3
  • 11
  • Holy crap. Took me ages to find this. Why do all of their HTTP examples in the documentation use `camelCase`? – PitaJ Aug 17 '22 at 18:15