Graph API Upload a file in to Teams Channel Via Share point folder || issue with ".docx,.doc,.ppt"

lnarayana 16 Reputation points
2022-04-06T11:47:36.693+00:00

Hi Team, I am working on uploading attachments to the team's channel by using graph API, As per the document we have uploaded files to the share point folder and then get the details of attachments and construct the attachment details as objects to send messages to team's channel.

As per my observation if we try to update txt, pdf it is working as expected but with the ".docx,.doc,.ppt etc..," we are facing an issue i.e web URL is provided by API for "pdf/txt" content if different for ".doc, .docx"

After posted to Share point folder :

txt/doc : weburl is "https://italentcorporation1.sharepoint.com/sites/ICS-DEV/Shared%20Documents/General/sample.pdf"
docx/doc : web url is "https://italentcorporation1.sharepoint.com/sites/ICS-DEV/_layouts/15/Doc.aspx?sourcedoc=%7B484E72FC-1E20-406B-92B4-B287591FA3CA%7D&file=sample1.doc&action=default&mobileredirect=true"

Steps to Reproduce the issue.

Step 1: Upload a file in to share folder drive with PUT API (https://graph.microsoft.com/v1.0/groups/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*/drive/items/\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*:/sample1.doc:/content
file is : sample1.doc

Once upload to share point Response is like below:

Response :
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#groups('ef3a57f4-ed3f-4970-a4f7-28587ab65774')/drive/items/$entity",
"@microsoft.graph.downloadUrl": "https://italentcorporation1.sharepoint.com/sites/ICS-DEV/_layouts/15/download.aspx?UniqueId=484e72fc-1e20-406b-92b4-b287591fa3ca&Translate=false&tempauth=eyJ0eXAiOiJKV1QiLCJhbGciOiJub25lIn0.eyJhdWQiOiIwMDAwMDAwMy0wMDAwLTBmZjEtY2UwMC0wMDAwMDAwMDAwMDAvaXRhbGVudGNvcnBvcmF0aW9uMS5zaGFyZXBvaW50LmNvbUBlNTVmZTA4YS1lNGUzLTQ2MjctYWQ0MC1kOTk2MWM2MTJhYWEiLCJpc3MiOiIwMDAwMDAwMy0wMDAwLTBmZjEtY2Uw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a3F4OD0&ApiVersion=2.0",
"createdDateTime": "2022-04-06T11:13:38Z",
"eTag": "\"{484E72FC-1E20-406B-92B4-B287591FA3CA},1\"",
"id": "01E5HGNWX4OJHEQIA6NNAJFNFSQ5MR7I6K",
"lastModifiedDateTime": "2022-04-06T11:13:38Z",
"name": "sample1.doc",
"webUrl": "https://italentcorporation1.sharepoint.com/sites/ICS-DEV/_layouts/15/Doc.aspx?sourcedoc=%7B484E72FC-1E20-406B-92B4-B287591FA3CA%7D&file=sample1.doc&action=default&mobileredirect=true",
"cTag": "\"c:{****************},1\"",
"size": 607,
"createdBy": {
"application": {
"id": "********************",
"displayName": "icsdevaad"
},
"user": {
"email": "***********************",
"id": "**********************************",
"displayName": "Lakshmi Narayana Natte"
}
},
"lastModifiedBy": {
"application": {
"id": "******************",
"displayName": "test"
},
"user": {
"email": "lnarayana8**************",
"id": "*******************",
"displayName": "Lakshmi Narayana Natte"
}
},
"parentReference": {
"driveId": "*****************",
"driveType": "documentLibrary",
"id": "*******************",
"path": "/drive/root:/General"
},
"file": {
"mimeType": "application/msword",
"hashes": {
"quickXorHash": "*****************="
}
},
"fileSystemInfo": {
"createdDateTime": "2022-04-06T11:13:38Z",
"lastModifiedDateTime": "2022-04-06T11:13:38Z"
}
}

Step 3: We have to perform a post call to send message in teams with attachement
As per document we have grab the our attachment ID is the GUID in the eTag of the driveItem, your attachment contentURL is the webUrl of the driveItem's

Request Body :

POST-https://graph.microsoft.com/v1.0/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=\"*********************\"></attachment>"
},
"attachments": [
{
"id": "*************************",
"contentType": "reference",
"contentUrl": "https://italentcorporation1.sharepoint.com/sites/ICS-DEV/_layouts/15/Doc.aspx?sourcedoc=%7B484E72FC-1E20-406B-92B4-B287591FA3CA%7D&file=sample1.doc&action=default&mobileredirect=true"",
"name": "sample1.doc"
}
]
}

Response is :

{
"error": {
"code": "BadRequest",
"message": "File contentUrl extension: '.doc&action=default&mobileredirect=true' and name extension: '.doc' do not match.",
"innerError": {
"date": "2022-04-06T11:15:02",
"request-id": "af37a0e8-9035-4c2c-89a8-376b61356b5e",
"client-request-id": "af37a0e8-9035-4c2c-89a8-376b61356b5e"
}
}
}

190573-screenshot-2022-04-06-at-44531-pm.png

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,565 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Srinivasa Rao Darna 6,686 Reputation points Microsoft Vendor
    2022-04-06T13:13:35.907+00:00

    Hi @lnarayana ,

    Please use full path till document in contentUrl:https://contoso.sharepoint.com/sites/test/Shared%20Documents/General/test%20doc.docx as mentioned in the example.

    Example: Send a message with file attachment

    Using the snippet from example I am able to successfully post a message with file attachment.

    POST https://graph.microsoft.com/v1.0/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"  
            }  
        ]  
    }  
    

    Result of my test:

    190593-802425-teams-channel-message-attachments.jpg

    Hope this helps.

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

    1 person found this answer helpful.