Issue with send message with file to MS Teams Channel using graph api

Pradip Kumbhar 111 Reputation points
2021-03-24T12:32:04.223+00:00

I am trying to send csv file to MS teams channel using graph API with below document;

https://learn.microsoft.com/en-us/graph/api/chatmessage-post?view=graph-rest-beta&tabs=http

This is working but I want to skip file uploading to SharePoint and send it directly to the channel by using the following document but getting error message:

https://learn.microsoft.com/en-us/graph/api/resources/chatmessageattachment?view=graph-rest-1.0#:~:text=application%2Fvnd.,%2Cbuttons%2C%20and%20input%20fields

In this document as it is mentioned to give content-type as "file" and contentUrl with base64 encoding of the file. Below is my code:

Method: POST

EndPoint: https://graph.microsoft.com/v1.0/teams/{teamId}/channels/{channel id}/messages

Request Body:

{
"body": {
"contentType": "html",
"content": "Here's the latest budget. <attachment id=\"1\"></attachment>"
},
"attachments": [
{
"id": "1",
"contentType": "file",
"contentUrl": "U2VudCBTdW5kYXkgYXQgMDg6MzA6MjYKCldlZWtseSBTdGF0dXMgUmVwb3J0ClRvdGFsIE91dHN0YW5kaW5nIFJlcXVlc3QsMTcKQ2xvc2VkIHRoaXMgd2VlaywyClJlcXVlc3QgT3BlbiBNb3JlIFRoYW4gNSAgIGRheXMsMTcKVW5hc3NpZ25lZCwxCgpZRFQKQ2xvc2VkIFJlcXVlc3RzLDI0Ck9wZW4gTW9yZSBUaGFuIDUgZGF5cywxNwoKQXNzaWduZWUsVG90YWwgIyBvdXRzdGFuZGluZyBvZiByZXF1ZXN0LENsb3NlZCB0aGlzIHdlZWssT3BlbiBtb3JlIHRoYW4gNSBkYXlzCiwxLDAsMApKYWNrIFF1aW50b24sNSwwLDUKSmFzb24gUGFsb21pbm8sMTEsMiwxMQpWZW5rYXQgQnVsdXN1LDEsMCwxCg==",
"name": "report4.csv",
"content": ""
}
]
}
Response:

{
"error": {
"code": "BadRequest",
"message": "Failed to process content in attachment with Id '1'.",
"innerError": {
"date": "2021-03-24T05:50:12",
"request-id": "baeab6b4-92f9-4a56-b8d9-9697fd0d4423",
"client-request-id": "f0190d39-1e83-63f2-ec50-ddba0078f15f"
}
}
}

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,006 questions
Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
3,549 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Diana Wanjuhi 1,376 Reputation points
    2021-03-25T13:00:53.14+00:00

    Hi @Pradip Kumbhar in the meantime, you could add the file to Teams via the client and get the url then reference that in the contentUrl field.

    I hope this helps,

    Diana.


  2. Nikitha-MSFT 541 Reputation points
    2021-04-14T19:14:10.737+00:00

    Copying answer from comments for better understanding

    Finally, I am able to figure out the issue, there was an issue with View ID and Content URL.

    The vieweId should be documented source ID, it will be visible in browser with the label "sourcedoc".

    In the case of the root site, we need to provide /:x:/r/ in addition to the file path

    Correct URL Path:
    https://nowlabcorp.sharepoint.com/:x:/r/Shared%20Documents/report.csv

    please check :https://learn.microsoft.com/en-us/answers/questions/335233/sorry-this-file-has-been-deleted-or-moved-teams-gr.html

    0 comments No comments

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.