Microsoft Graph API add attachment to a message from another message - microsoft-graph-api

I am trying to "clone" a message from a user's mailbox to create a copy in a different mailbox. I could successfully use "create message" API by "cloning" a messages's parts into new message including attachments. However, considering 4MB limitation on REST payload, messages that are with larger than 4MB are rejected. So, wondering if there is a way to include attachment by attachment id or contentId instead of downloading and uploading as the attachment is already available on the server.

According to your description, I assume you want to add attachment to a message.
Based on my test, we can use this API to implement it.
'POST /users/{id | userPrincipalName}/messages/{id}/attachments'
The request body is a JSON representation of Attachment object like this:
{
"contentType": "string",
"id": "string (identifier)", // attachment id
"isInline": true,
"lastModifiedDateTime": "String (timestamp)",
"name": "string",
"size": 1024
}
For more detail, we can refer to this document.

Just in case you have not found the answer.
Using the Microsoft Graph API, you can attach files up to 150 MB to an Outlook message or event item. Depending on the file size, choose one of two ways to attach the file:
If the file size is under 3 MB, do a single POST on the attachments navigation property of the Outlook item; see how to do this for a message or for an event. The successful POST response includes the ID of the file attachment.
If the file size is between 3MB and 150MB, create an upload session, and iteratively use PUT to upload ranges of bytes of the file until you have uploaded the entire file. A header in the final successful PUT response includes a URL with the attachment ID.
Please refer to this article for more details:
https://learn.microsoft.com/en-us/graph/outlook-large-attachments?tabs=http

Related

How do I get the mimetype and file ID using the Microsoft Graph API for OneDrive?

We are integrating our product with MS OneDrive. Our users will connect their MS accounts and choose a folder that they want us to pull files from. Our system only supports certain file types so we have to get the mimetype information AND the file ID after making a request. But we can't find a request that brings back both pieces of information so we can determine if we should pull a file or not.
This is close, but it doesn't give us the mimetype information.
https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/drive_recent?view=odsp-graph-online
Anyone have any ideas?
The get method lets you retrieve all kinds of information about a file: https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_get?view=odsp-graph-online
It returns a driveItem object, that has a file property. And that file property has a mimeType property. See here https://learn.microsoft.com/en-us/onedrive/developer/rest-api/resources/driveitem?view=odsp-graph-online and here https://learn.microsoft.com/en-us/onedrive/developer/rest-api/resources/file?view=odsp-graph-online.
Unfortunately that means, that you will need more than one request to get the information you want.
If filtering by filename suffix is good enough for you, you can query OneDrive like this:
POST /search/query
Content-Type: application/json
{
"requests": [
{
"entityTypes": [
"driveItem"
],
"query": {
"queryString": "filetype:docx OR filetype:doc"
}
}
]
}
Documentation: https://learn.microsoft.com/en-us/graph/search-concept-files#example-5-use-filters-in-search-queries

ms botframework attachment can not generate sharelink

I use botframework api to send message to users.
the message contains attachment.
So I send the activity like this
{
"attachments": [{
"contentType": "application/vnd.microsoft.teams.card.file.info",
"contentUrl": "https://contoso.sharepoint.com/personal/johnadams_contoso_com/Documents/Applications/file_example.txt",
"name": "file_example.txt",
"content": {
"uniqueId": "1150D938-8870-4044-9F2C-5BBDEBA70C8C",
"fileType": "txt",
}
}]
}
And I send bot message to user like this
And I want to generate sharelink by clicking this button
But I got a error that told me I can's generate sharelink
Does anyone knows the reason?
This issue is confirmed by the original post that it's caused by incorrect uniqueID. The uniqueID is not the itemID in one drive.
For more information about different IDs for botframework, please refer to document here: https://learn.microsoft.com/en-us/previous-versions/azure/bot-service/bot-service-resources-identifiers-guide?view=azure-bot-service-3.0

Message not found in user mailbox while working on microsoft graph explorer

Auto Remediation not happening. Error :- message not present in User's mailbox .
Scenario:
While working on microsoft graph explorer and sending mail with attachment the outlook is not able to auto remediate the message and while checking the url on graph explorer i am not getting any value associated with it and also i am getting the error like the message is not there in the user mailbox.
The Attachment name which I am using are double byte Chinese characters (发送邮.xml)
Instead of this if I am using attachment name like this (for e.g 送邮.xml) then it is working as expected and the mail is successfully getting remediated.
Is there any bit size limit of attachment names in graph explorer because of which it is not taking that attachment name(发送邮.xml) whereas it is working fine with (送邮.xml)
Please help me with this.
I have tried giving the payload this way and used the same name that didn't work for you. But I was able to success send mail using Graph Explorer.
"attachments": [
{
"#odata.type": "#microsoft.graph.fileAttachment",
"name": "发送邮.xml",
"contentType": "text/plain",
"contentBytes": "SGVsbG8gV29ybGQh"
}
]

How do I get a usable email message reference id from headers to use in a Microsoft Graph query?

I am trying to use the references message ID in email headers of a forwarded email in Office 365 to then find the original email that has that message ID.
The references ID in the forwarded email's headers would show something like what's shown below. Note I'm obtaining these headers from Microsoft Graph by adding &$select=internetMessageHeaders to my query using sender/subject to find the forwarded email.
{
"name": "References",
"value": "<CAOLK2SAEzvs=-6Rs5cTbgORNivQVK2AvMJDJT8o+ghx-XhCn7w#mail.gmail.com>"
}
However, if I take this and use it in a query like https://graph.microsoft.com/v1.0/me/messages?$filter=internetMessageId eq '<CAOLK2SAEzvs=-6Rs5cTbgORNivQVK2AvMJDJT8o+ghx-XhCn7w#mail.gmail.com>'
I do not receive any results.
If I hunt down the original email in Microsoft Graph using sender and subject, I see the following for the internetMessageId field for the original email:
{
"name": "Message-ID",
"value": "<CAOLK2SC6s9Me05kYRqeuEHKK8DrQbXx34ScT0AzGOFFLMdRapg#mail.gmail.com>"
}
Does anyone have any ideas on what Office 365 or Microsoft Graph is doing to change CAOLK2SAEzvs=-6Rs5cTbgORNivQVK2AvMJDJT8o+ghx-XhCn7w into CAOLK2SC6s9Me05kYRqeuEHKK8DrQbXx34ScT0AzGOFFLMdRapg? It looks like some sort of encoding, but I haven't been able to reproduce it.
Ultimately I would like to be able to transform what I'm getting in the references ID header such that I can turn around and use that ID to find the original email using Microsoft Graph.
The id is a hashed value that, among other things, includes the path/folder that the message resides in. So if you, for example, move a message (say from Inbox to Archive) then you should expect the id to change.
For tracking a message, regardless of its location, you should use the internetMessageId ($select=internetMessageId) property instead.
I discovered the issue. In my test message-ID I have the characters = and +
These characters must be URL encoded to %3D and %2B before being used in the $filter query. When they are URL encoded, I am able to find the original email using MS Graph.
In Microsoft Graph v1.0 and beta, you can obtain "immutable ids" by sending an additional HTTP header in your API requests:
Prefer: IdType="ImmutableId"
I've put immutable in italics as this is the term Microsoft uses. Be aware of the following caveats, the ID will change if:
The user moves the item to an archive mailbox;
The user exports the item (to a PST, as an MSG file, etc.) and re-imports it into their mailbox;
The user creates a draft which they later send;
You can also convert existing ids to immutable ones by the following request:
POST https://graph.microsoft.com/beta/me/translateExchangeIds
{
"inputIds" :
[
"AQMkAGM2…"
],
"targetIdType" : "restImmutableEntryId",
"sourceIdType" : "restId"
}
Which will give you the response:
HTTP 200 OK
{
"#odata.context": "https://graph.microsoft.com/beta/$metadata#Collection(microsoft.graph.convertIdResult)",
"value": [
{
"targetId": "AAkALgAA...",
"sourceId": "AQMkAGM2..."
}
]
}

Download shared file with Microsoft Graph API

I have to get data from a excel document which is shared with me. I have to get the cell colors of a lot of cells which takes very long with the graph api since you can only get them cell by cell. So i would like to download it and process it offline.
However since the file is not on my drive I'm not getting the #microsoft.graph.downloadUrl field with the file details request, and I would need to go with the Download File endpoint, which unfortunately returns 404. Is there any suggestion how can this be done?
According to your questions, you want to download a file which is someone shared with you. If I have misunderstand your, please let me know.
I have a preliminary solution like this:
First, we can use the List items shared with the signed-in user API to get the file which is shared with you. The part of the response like this:
{
"id": "1312abc",
"remoteItem": {
"id": "{itemid}",
"name": "March Proposal.docx",
"file": { },
"size": 19121,
"parentReference": {
"driveId": "{driveid}",
"id": "1991210caf!104"
}
}
}
}
From the response, we can get the drive's id and the item's id of the shared file.
Second, we can use the drive's id and the item's id to download the file. The API like this:
GET /drives/{drive-id}/items/{item-id}/content
For more detail, we can refer to the content of Download the contents of a DriveItem

Resources