Send a message to Microsoft Teams using Microsoft Graph in SPFx

David RamalhoMicrosoft 3654 years ago36 Views

A few days ago, I got a challenge that required me to send a message from a SharePoint page into a Microsoft Teams chat and that can be achieved using Microsoft Graph. I created a blank SPFx project and this post will go to the most important elements. You can also take a look at a sample webpart that I built using this process.

First, you need to add to the package-solution.json the required scopes for the Microsoft Graph can make the required calls. These are the scopes:

{
    "resource": "Microsoft Graph",
    "scope": "ChatMessage.Send"
},
{
    "resource": "Microsoft Graph",
    "scope": "Chat.Create"
},
{
    "resource": "Microsoft Graph",
    "scope": "Chat.ReadWrite"
},
{
    "resource": "Microsoft Graph",
    "scope": "User.Read"
},
{
    "resource": "Microsoft Graph",
    "scope": "User.ReadWrite.All"
},
{
    "resource": "Microsoft Graph",
    "scope": "Directory.Read.All"
},
{
    "resource": "Microsoft Graph",
    "scope": "Directory.ReadWrite.All"
}

For this webpart to be able to make the calls, you will need to approve this scopes on your SharePoint. Then we need to make 4 calls to Microsoft Graph:

  1. Get the user Id of the user that is sending the message. Get a user – Microsoft Graph v1.0 | Microsoft Docs
    • GET => https://graph.microsoft.com/v1.0/me
  2. Get the user Id of the user that will receive the message. Get a user – Microsoft Graph v1.0 | Microsoft Docs
    • GET => https://graph.microsoft.com/v1.0/users/ {id | userPrincipalName}
  3. Get the chat Id, behind the scenes, a connection between 2 users contains a chat, and we need to get their chat Id. Note that if a chat already exists it will add a message on top of the existing chat. Create chat – Microsoft Graph beta | Microsoft Docs
    • POST => https://graph.microsoft.com/beta/chats
  4. Send the message to the user. Send chatMessage in a channel or a chat – Microsoft Graph beta | Microsoft Docs
    • POST => https://graph.microsoft.com/beta/chats/{id}/messages

You can check all the required code on the following link.

The post Send a message to Microsoft Teams using Microsoft Graph in SPFx appeared first on SharePoint Tricks.

Original Post https://sharepoint-tricks.com/send-a-message-to-microsoft-teams-using-microsoft-graph-in-spfx/

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Leave a reply

Join Us
  • X Network2.1K
  • LinkedIn3.8k
  • Bluesky0.5K
Support The Site
Events
March 2025
MTWTFSS
      1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31       
« Feb   Apr »
Follow
Sign In/Sign Up Sidebar Search
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...