Create a Teams calendar event from a bot

Brass Contributor

Hi,

I have a nodejs bot running with my Teams app. I would link to add a new meeting event in Teams calendar, is it possible? I have read about 'schedules' in Graph API, but I am not sure if this is the key, since I can not see fields like startTime, duration, ...

Could you please help me?

Thanks in advance,

Diego

16 Replies

@diegoSpace - Could you please try Graph API to create calendar event.

@Nikitha-MSFT Thanks for your response.

 

I have some doubts, please correct what I write below if required:

 

As far as I understand, I can use Application permission to create an event, so I will access Graph without a user (https://docs.microsoft.com/en-us/graph/auth-v2-service)

 

I will use 'POST /users/{id}/events', where the {id} must be Teams userId (what I get in 'activity.from.id' of an incoming command), and not the Office365/Azure id.

 

'Bearer' header is required, so I need a token. I have running code to get Teams users, and previously I get a bearer token with this options:

 

var options = {
    uri: 'https://login.microsoftonline.com/{tenant ID}/oauth2/v2.0/token',
    method: 'POST',
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    body: 'client_id=' + {appId} + '&client_secret=' + {appPassword} + '&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&grant_type=client_credentials',
};

 

With the token received in the response, I can access to the users in a Teams group:
      https://graph.microsoft.com/v1.0/groups/{groupID}/members

 

Can I use this token (for my application) to create an event for a user from the bot?

 

With a userId = '29:184h5dKIrOfsVklZrZyWMe91Gio.....nNQiCqFNLreLIEq-YC' I have tried this PUT:
      https://graph.microsoft.com/v1.0/users/{userId}/calendar/events
      and the previous Authentication bearer token
      with an example event as body

 

but I get a '500 Internal Server Error' as response.

 

What am I doing wrong?

Thanks a lot,

Diego

@diegoSpace - {id} should be user azure id or UPN. Could you please try using the azure id or UPN.

@Nikitha-MSFT Thanks, Using the Azure id i get this error response:

 

"code": "ErrorInvalidRequest",
"message": "The OData request is not supported.",

 

 

 

@diegoSpace - Could you please share your request body what you are trying?

@Nikitha-MSFT I used the example body from

https://docs.microsoft.com/en-us/graph/api/user-post-events?view=graph-rest-1.0&tabs=http

 

(updated date & timezone)

 

{
     "subject": "Let's go for lunch",
     "body": {
          "contentType": "HTML",
          "content": "Does late morning work for you?"
     },
     "start": {
          "dateTime": "2020-04-15T12:00:00",
          "timeZone": "Europe/Madrid"
     },
     "end": {
          "dateTime": "2020-04-15T14:00:00",
          "timeZone": "Europe/Madrid"
     },
     "location":{
          "displayName":"Harry's Bar"
     },
     "attendees": [
          {
               "emailAddress": {
                    "address":"samanthab@contoso.onmicrosoft.com",
                    "name": "Samantha Booth"
               },
               "type": "required"
          }
     ]
}     

@diegoSpace , Do you have required Calendar.ReadWrite permission in your access token? You can check that on jwt.io.

@Trinetra-MSFT Thanks for your response, on jwt.io I could see my permissions, and there was no permission for Calendar. I added it in my app (Azure) and then retried:

 "roles": [
        "User.ReadWrite.All",
        "Group.Read.All",
        "Directory.ReadWrite.All",
        "Directory.Read.All",
        "User.Read.All",
        "GroupMember.Read.All",
        "Calendars.ReadWrite"
],

 

But with that token I still have the error:

     "The OData request is not supported."

 

Is "Calendars.ReadWrite" permission the only required? It is without '.All' (like the others), I have not found the '.All' one...

 

My raw request is:

 

PUT /v1.0/users/bcb3f5a......23a5925/calendar/events HTTP/1.1
Authorization: Bearer eyJ0eXAiOiJKV1Q.....EUquw
Host: graph.microsoft.com
Content-Type: application/json
Content-Length: 548

{
"subject": "Let's go for lunch",
"body": {
"contentType": "HTML",
"content": "Does late morning work for you?"
},
"start": {
"dateTime": "2020-04-15T12:00:00",
"timeZone": "Europe/Madrid"
},
"end": {
"dateTime": "2020-04-15T14:00:00",
"timeZone": "Europe/Madrid"
},
"location":{
"displayName":"Harry's Bar"
},
"attendees": [
{
"emailAddress": {
"address":"samanthab@contoso.onmicrosoft.com",
"name": "Samantha Booth"
},
"type": "required"
}
]
}

 

Thanks a lot,

Diego

 

@diegoSpace , I see you are hitting the Put request , Ideally it should be POST:

POST /v1.0/users/{UserPrincipleName}/calendar/events. Try with this and let me know, how it goes,

Please take a look at Example 5.

@Trinetra-MSFT Thanks for your response, that was the problem. I don't know why i was using PUT (i think I see it somewhere), but event documentation says clearly 'POST'.

 

By the way, which version of Graph API should I used? 1.0 or beta?

 

Thanks a lot!

Diego

@diegoSpace , If you are looking to Create a Event in user calendar you can use v1.0 but if you want to get the joinWebUrl too than go for beta version. create event with online meeting provider api is in beta version.

@Trinetra-MSFT Thanks. Is it possible to open Teams Calendar view programatically (or even recently created event) once I receive the event creation response? My Teams event is created from a Messaging extension dialog, and I would like to open it once created

@diegoSpace , I've not seen this but I assume that you can try to deep link the webLink Url with Teams

@Trinetra-MSFT If I use microsoftTeams.executeDeepLink(webLink) in my Teams task the created event is open but in a browser (not in Teams client I mean):
         https://outlook.office365.com/calendar/item/...

The webLink is: https://outlook.office365.com/owa/?itemid=AAMkADBiNDA2...AAA%3D&exvsurl=1&path=/calendar/item

 

Thanks,

Diego

@diegoSpace , That is a webLink for sure it will work in web only, If you want to open within Teams you can use joinWebUrl that will open within Teams Desktop client. 

Go to the Group you want the calendar for:
Click “Calendar”:
Copy the URL from your browser:
Add a new tab within a Channel in Teams:
Select “Website”:
Add a name and the URL you copied from the Outlook Web App:
Sign in to Microsoft when prompted.