You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Graph API批量添加小文件到任务报错:taskFileAttachment类型无法识别

问题解决:Graph API添加待办任务附件时类型解析错误

你尝试通过以下C#代码给待办任务添加多个小文件附件并提交到Graph API:

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);

运行后出现错误:

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.

虽然参考文档认为TaskFileAttachment是正确类型,但待办任务的附件操作逻辑和邮件不同,问题出在待办任务不支持创建时一并添加附件,必须分两步操作:

解决方案

  1. 先创建不带附件的待办任务,获取任务ID
  2. 针对已创建的任务单独添加每个附件

修正后的代码示例

// 第一步:创建不带附件的任务
var createdTask = await _graphServiceClient.Users[idUser].Todo.Lists[idList].Tasks.Request().AddAsync(_newTask);

// 第二步:为已创建的任务添加附件
if (attachments != null && attachments.Length > 0)
{
    foreach (var _attachment in attachments)
    {
        var fileAttachment = new TaskFileAttachment
        {
            Name = _attachment.FileName,
            ContentBytes = _attachment.ContentBytes,
            ContentType = _attachment.ContentType
        };
        await _graphServiceClient.Users[idUser].Todo.Lists[idList].Tasks[createdTask.Id].Attachments.Request().AddAsync(fileAttachment);
    }
}

额外注意事项

  • 确保使用Beta版本的Graph SDK,待办任务附件接口目前仅在Beta版本中提供支持
  • 邮件附件支持创建时一并添加,但待办任务的附件接口设计不同,必须分开执行操作

内容的提问来源于stack exchange,提问作者Salvador Ruiz Guevara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 16:30:58