Search code examples
azurepowershellmicrosoft-graph-apimicrosoft-teams

How to declare user@odata.bind when using Powershell with MS Graph to send a Teams message


I am trying to send a Teams message using PowerShell with MS Graph and getting the error message below whenever the New-MgChat function is called

New-MgChat : 'user@odata.bind' field is missing in the request.

I have the code gone thru several iterations from what I find and even from the MS API resource itself and it seems the declaration for 'members' which has the user@odata.bind is correct but still getting an error

Import-Module Microsoft.Graph.Teams

Connect-MgGraph -NoWelcome -Scopes "Chat.ReadWrite", "User.Read"

$params = @{
    chatType = "oneOnOne"
    members = @(
        @{
            "@odata.type" = "#microsoft.graph.aadUserConversationMember"
            roles = @(
                "owner"
            )
            "user@odata.bind" = "https://graph.microsoft.com/v1.0/users('XXXXXXXX-XXX-XXXX-XXXX-XXXXXXXXXXXX')"
        }
        @{
            "@odata.type" = "#microsoft.graph.aadUserConversationMember"
            roles = @(
                "owner"
            )
            "user@odata.bind" = "https://graph.microsoft.com/v1.0/users('XXXXXXXX-XXX-XXXX-XXXX-XXXXXXXXXXXX')"
        }
    )
}

$myChatSession = New-MgChat -BodyParameter $params


$Body = @{
    ContentType = 'Text'
    Content = 'Test Chat Message'
  }
  
New-MgChatMessage -ChatId $myChatSession.id -Body $Body

Solution

  • The body of your request looks correct, not an answer to your problem but perhaps a workaround, a direct API call to Create chat and Send message in chat might give a better indication of what went wrong or if the cmdlet is the problem.

    Connect-MgGraph -NoWelcome -Scopes 'Chat.ReadWrite', 'User.Read'
    
    # Create chat
    $myChatSession = Invoke-MgGraphRequest POST 'v1.0/chats' -Body @{
        chatType = 'oneOnOne'
        members  = @(
            @{
                '@odata.type'     = '#microsoft.graph.aadUserConversationMember'
                roles             = @('owner')
                'user@odata.bind' = "https://graph.microsoft.com/v1.0/users('XXXXXXXX-XXX-XXXX-XXXX-XXXXXXXXXXXX')"
            }
            @{
                '@odata.type'     = '#microsoft.graph.aadUserConversationMember'
                roles             = @('owner')
                'user@odata.bind' = "https://graph.microsoft.com/v1.0/users('XXXXXXXX-XXX-XXXX-XXXX-XXXXXXXXXXXX')"
            }
        )
    }
    
    # Send message in chat
    Invoke-MgGraphRequest POST "v1.0/chats/$($myChatSession.id)/messages" -Body @{
        body = @{
            content = 'Hello world!'
        }
    }