Have the following code.
if (attachments != null)
{
if (attachments.Length > 0)
{
_newTask.Attachments = new TodoTaskAttachmentsCollectionPage();
foreach (var _attachment in attachments)
{
_newTask.Attachments.Add(new TaskFileAttachment
{
Name = _attachment.FileName,
ContentBytes = _attachment.ContentBytes,
ContentType = _attachment.ContentType
});
}
}
}
await _graphServiceClient.Users[idUser].Todo.Lists[idList].Tasks.Request().AddAsync(_newTask);
Im trying to add multiple small files to a task and then post it to the graph api.
But it results in the following error:
One or more errors occurred. (One or more errors occurred. (A type named 'microsoft.toDo.taskFileAttachment' could not be resolved by the model. When a model is available, each type name must resolve to a valid type.
Basically saying that the type taskFileAttachment
is not the correct type to add to the collection of attachments of a task
.
But, according to MSdoc that's the correct type to add.
Cant see what i'm missing and there is not a lot of documentation of how to post small files to a task. I already done it through the api for mails and thought it was really straightforward but it looks as it is not the case.
CodePudding user response:
As per the docs , there are list of property that are required when you create the todoTask , in that you can't find any property to attach the file
First try to create a new listItems , and attach the file
Sample to attach the file
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var attachmentBase = new TaskFileAttachment
{
Name = "smile",
ContentBytes = Convert.FromBase64String("a0b1c76de9f7="),
ContentType = "image/gif"
};
await graphClient.Me.Todo.Lists["{todoTaskList-id}"].Tasks["{todoTask-id}"].Attachments
.Request()
.AddAsync(attachmentBase);
CodePudding user response:
So as they commented in my original post, there is no current way to attach a collection of files in one single request. They must attached to a created task one at a time.
CodePudding user response:
You can use the CreateUploadSession and UploadChunk endpoints to upload the files in chunks. Here is an example of how you can do this.
var createSessionResponse = await graphClient.Me.Drive.Root.ItemWithPath("path folder").CreateUploadSession().Request().PostAsync();
// Get the upload URL and the next expected range from the response
string uploadUrl = createSessionResponse.UploadUrl;
string nextExpectedRanges = createSessionResponse.NextExpectedRanges;
// Open a stream to the file you want to upload
using (FileStream fileStream = new FileStream("path/to/file", FileMode.Open))
{
// Calculate the size of each chunk
long chunkSize = 4 * 1024 * 1024; // 4 MB
// Calculate the number of chunks
long numberOfChunks = (long)Math.Ceiling((double)fileStream.Length / chunkSize);
// Create a byte array to hold the contents of each chunk
byte[] chunk = new byte[chunkSize];
// Iterate through the chunks
for (long i = 0; i < numberOfChunks; i )
{
// Read the chunk from the stream
int bytesRead = fileStream.Read(chunk, 0, chunk.Length);
// Calculate the range of the current chunk
string currentChunkRange = $"bytes {i * chunkSize}-{i * chunkSize bytesRead - 1}/{fileStream.Length}";
// Update the next expected ranges to include the current chunk range
nextExpectedRanges = $"{currentChunkRange},{nextExpectedRanges}";
// Create a ByteArrayContent object from the chunk
ByteArrayContent byteArrayContent = new ByteArrayContent(chunk, 0, bytesRead);
// Set the Content-Range header for the current chunk
byteArrayContent.Headers.Add("Content-Range", currentChunkRange);
// Upload the chunk using the UploadChunk endpoint
HttpResponseMessage uploadChunkResponse = await graphClient.Me.Drive.Root.ItemWithPath("path/to/folder").ItemWithPath("file.txt").CreateUploadSession(uploadUrl).Request().PutAsync(byteArrayContent);
}
}