Create taskFileAttachment
Article
09/27/2023
2 contributors
Feedback
In this article
Namespace: microsoft.graph
Add a new taskFileAttachment object to a todoTask .
This operation limits the size of the attachment you can add to under 3 MB. If the size of the file attachments is more than 3 MB, create an upload session to upload the attachments.
This API is supported in the following national cloud deployments .
Global service
US Government L4
US Government L5 (DOD)
China operated by 21Vianet
✅
✅
✅
❌
Permissions
One of the following permissions is required to call this API. To learn more, including how to choose permissions, see Permissions .
Permission type
Permissions (from least to most privileged)
Delegated (work or school account)
Tasks.ReadWrite
Delegated (personal Microsoft account)
Tasks.ReadWrite
Application
Tasks.ReadWrite.All
HTTP request
POST /me/todo/lists/{id}/tasks/{id}/attachments
POST /users/{id}/todo/lists/{id}/tasks/{id}/attachments
Name
Description
Authorization
Bearer {token}. Required.
Content-Type
application/json. Required.
Request body
In the request body, supply a JSON representation of the taskFileAttachment object.
When you create a file attachment, include "@odata.type": "#microsoft.graph.taskFileAttachment"
and the required properties.
Property
Type
Description
contentBytes
Binary
The base64-encoded contents of the file. Required.
contentType
String
The content type of the attachment.
name
String
The name of the text displayed under the icon that represents the embedded attachment. This does not need to be the actual file name. Required.
size
Int32
The size in bytes of the attachment.
Response
If successful, this method returns a 201 Created
response code and a taskFileAttachment object in the response body.
Examples
Request
The following is an example of a request.
POST https://graph.microsoft.com/v1.0/me/todo/lists/AAMkpsDRVK=/tasks/AAKdfjhgsjhgJ=/attachments
Content-Type: application/json
{
"@odata.type": "#microsoft.graph.taskFileAttachment",
"name": "smile",
"contentBytes": "a0b1c76de9f7=",
"contentType": "image/gif"
}
// Code snippets are only available for the latest version. Current version is 5.x
var graphClient = new GraphServiceClient(requestAdapter);
var requestBody = new TaskFileAttachment
{
OdataType = "#microsoft.graph.taskFileAttachment",
Name = "smile",
ContentBytes = Convert.FromBase64String("a0b1c76de9f7="),
ContentType = "image/gif",
};
var result = await graphClient.Me.Todo.Lists["{todoTaskList-id}"].Tasks["{todoTask-id}"].Attachments.PostAsync(requestBody);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
// THE CLI IS IN PREVIEW. NON-PRODUCTION USE ONLY
mgc users todo lists tasks attachments create --user-id {user-id} --todo-task-list-id {todoTaskList-id} --todo-task-id {todoTask-id} --body '{\
"@odata.type": "#microsoft.graph.taskFileAttachment",\
"name": "smile",\
"contentBytes": "a0b1c76de9f7=",\
"contentType": "image/gif"\
}\
'
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
//other-imports
)
graphClient := msgraphsdk.NewGraphServiceClientWithCredentials(cred, scopes)
requestBody := graphmodels.NewAttachmentBase()
name := "smile"
requestBody.SetName(&name)
contentBytes := []byte("a0b1c76de9f7=")
requestBody.SetContentBytes(&contentBytes)
contentType := "image/gif"
requestBody.SetContentType(&contentType)
attachments, err := graphClient.Me().Todo().Lists().ByTodoTaskListId("todoTaskList-id").Tasks().ByTodoTaskId("todoTask-id").Attachments().Post(context.Background(), requestBody, nil)
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
TaskFileAttachment attachmentBase = new TaskFileAttachment();
attachmentBase.name = "smile";
attachmentBase.contentBytes = Base64.getDecoder().decode("a0b1c76de9f7=");
attachmentBase.contentType = "image/gif";
graphClient.me().todo().lists("AAMkpsDRVK=").tasks("AAKdfjhgsjhgJ=").attachments()
.buildRequest()
.post(attachmentBase);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
const options = {
authProvider,
};
const client = Client.init(options);
const attachmentBase = {
'@odata.type': '#microsoft.graph.taskFileAttachment',
name: 'smile',
contentBytes: 'a0b1c76de9f7=',
contentType: 'image/gif'
};
await client.api('/me/todo/lists/AAMkpsDRVK=/tasks/AAKdfjhgsjhgJ=/attachments')
.post(attachmentBase);
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
<?php
// THIS SNIPPET IS A PREVIEW VERSION OF THE SDK. NON-PRODUCTION USE ONLY
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new TaskFileAttachment();
$requestBody->setOdataType('#microsoft.graph.taskFileAttachment');
$requestBody->setName('smile');
$requestBody->setContentBytes(\GuzzleHttp\Psr7\Utils::streamFor(base64_decode('a0b1c76de9f7=')));
$requestBody->setContentType('image/gif');
$result = $graphServiceClient->me()->todo()->lists()->byTodoTaskListId('todoTaskList-id')->tasks()->byTodoTaskId('todoTask-id')->attachments()->post($requestBody)->wait();
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
Import-Module Microsoft.Graph.Users
$params = @{
"@odata.type" = "#microsoft.graph.taskFileAttachment"
name = "smile"
contentBytes = "a0b1c76de9f7="
contentType = "image/gif"
}
# A UPN can also be used as -UserId.
New-MgUserTodoListTaskAttachment -UserId $userId -TodoTaskListId $todoTaskListId -TodoTaskId $todoTaskId -BodyParameter $params
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
# THE PYTHON SDK IS IN PREVIEW. FOR NON-PRODUCTION USE ONLY
graph_client = GraphServiceClient(request_adapter)
request_body = TaskFileAttachment(
odata_type = "#microsoft.graph.taskFileAttachment",
name = "smile",
content_bytes = base64.urlsafe_b64decode("a0b1c76de9f7="),
content_type = "image/gif",
)
result = await graph_client.me.todo.lists.by_list_id('todoTaskList-id').tasks.by_task_id('todoTask-id').attachments.post(body = request_body)
For details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation .
Response
The following is an example of the response.
Note: The response object shown here might be shortened for readability.
HTTP/1.1 201 Created
Content-Type: application/json
{
"@odata.type": "#microsoft.graph.taskFileAttachment",
"id": "AAMkADNkN2R",
"lastModifiedDateTime": "2017-01-26T08:48:28Z",
"name": "smile",
"contentType": "image/gif",
"size": 1008
}