How to send an attachment file from Application to teams channel or user using Graph API

GP 41 Reputation points
2023-07-11T14:51:49.09+00:00

Hi,

I would like to send an attachment to teams channel or 1:1 chat from my application using Graph API delegate permissions.

I can not use the "contentURL" here.

I have to generate the file from app itself and send to teams.

How can I achieve this using Graph API?

or any other solution for this?

Please advise.

Thanks!

Microsoft Teams Development
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. TH-4749-MSFT 3,315 Reputation points
    2023-07-11T17:21:51.62+00:00

    Hello GP,

    Thanks for reaching out. You can achieve sending an app generated file attachment using the Graph APIs 'fileAttachment' resource's 'contentBytes' property. The property takes a Base64 string as input so you will need to convert the file contents to Base64 first before sending the attachment. You can find more information in article [https://learn.microsoft.com/en-us/graph/api/resources/fileattachment?view=graph-rest-1.0](https://learn.microsoft.com/en-us/graph/api/resources/fileattachment?view=graph-rest-1.0) and example of sending a message using fileAttachment and 'contentBytes' in article [https://learn.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http](https://learn.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http).
    

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    Thanks.

    1 person found this answer helpful.

  2. Sayali-MSFT 3,991 Reputation points Microsoft External Staff Moderator
    2023-07-12T11:43:44.66+00:00
    • If you want to send the attachment trough Graph API-
      The file must already be in SharePoint. To find the file properties, GET the driveItem for the file. For example, /drives/{id}/items/{id}. Your attachment ID is the GUID in the eTag of the driveItem, your attachment contentURL is the webUrl of the driveItem's folder plus the driveItem's name, and your attachment name is the driveItem's name.

    Post messages with card attachments that refer to existing SharePoint files, using the Graph APIs for OneDrive and SharePoint. To use the Graph APIs, obtain access to either of the following through the standard OAuth 2.0 authorization flow:

    • A user's OneDrive folder for personal and groupchat files.
      • The files in a team's channel for channel files.
      Graph APIs work in all Teams scopes. For more information, see send chat message file attachments. Alternately, you can send files to and receive files from a bot using the Teams bot APIs.
          POST https://graph.microsoft.com/beta/teams/fbe2bf47-16c8-47cf-b4a5-4b9b187c508b/channels/19:4a95f7d8db4c4e7fae857bcebe0623e6@thread.tacv2/messages
          Content-type: application/json
          
          {
              "body": {
                  "contentType": "html",
                  "content": "Here's the latest budget. <attachment id=\"153fa47d-18c9-4179-be08-9879815a9f90\"></attachment>"
              },
              "attachments": [
                  {
                      "id": "153fa47d-18c9-4179-be08-9879815a9f90",
                      "contentType": "reference",
                      "contentUrl": "https://m365x987948.sharepoint.com/sites/test/Shared%20Documents/General/test%20doc.docx",
                      "name": "Budget.docx"
                  }
              ]
          }
          
          ```
    
    
    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.