如何设置或删除completedDateTime

Color_yr 0 信誉分
2024-10-28T07:03:18.31+00:00

我已经获取到了listid和taskid,以及task里面的内容,我需要设置task是否已经完成,我尝试通过设置completedDateTime来实现

但是无论completedDateTime设置为何值时都不能生效

这是我的代码,需要修改哪里才能正常工作?

public static async Task<bool> EditTaskItem(string bear, string listId, string task, string? text, bool? isCheck, DateTime? time, string? body)
{
    var obj = new JObject();
    if (text != null)
    {
        obj.Add("title", text);
    }
    if (isCheck is { } check)
    {
        if (check)
        {
            obj.Add("completedDateTime", new JObject()
            {
                { "timeZone", "UTC" },
                { "dateTime", BuildTime(DateTime.UtcNow) }
            });
        }
        else
        {
            obj.Add("completedDateTime", null);
        }
    }
    if (time is { } time1)
    {
        obj.Add("dueDateTime", new JObject()
        {
            { "timeZone", "UTC" },
            { "dateTime", BuildTime(time1.ToUniversalTime()) }
        });
    }
    if (body != null)
    {
        obj.Add("body", new JObject()
        {
            { "contentType", "text" },
            { "content", body }
        });
    }
    var content = new StringContent(obj.ToString(), Encoding.UTF8, "application/json");
    var req = new HttpRequestMessage()
    {
        RequestUri = new Uri($"https://graph.microsoft.com/v1.0/me/todo/lists/{listId}/tasks/{task}"),
        Method = HttpMethod.Patch,
        Content = content
    };
    req.Headers.Add("Authorization", "Bearer " + bear);
    try
    {
        var res = await OAuth.Client.SendAsync(req);
        return res.StatusCode == HttpStatusCode.OK;
    }
    catch (Exception e)
    {
        Logs.Error("todo error", e);
        return false;
    }
}
Microsoft Graph
Microsoft Graph
一种 Microsoft 可编程性模型,用于公开 REST API 和客户端库以访问 Microsoft 365 服务上的数据。
43 个问题
0 个注释 无注释
{count} 票

3 个答案

排序依据: 非常有帮助
  1. Yakun Huang-MSFT 6,575 信誉分 Microsoft 供应商
    2024-10-28T09:00:24.97+00:00

    Hi @Color_yr

    根据Graph API,对于Todo tasks,仅提供了Create和List Task的端点,没有修改task的端点,因此无法使用Graph API对task的状态或completedDateTime属性进行修改,您只能通过To Do客户端进行修改。

    User's image

    更多详细信息请查看官方文档:

    https://learn.microsoft.com/en-us/graph/api/todotasklist-list-tasks?view=graph-rest-1.0&tabs=http

    Hope this helps.

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


  2. Color_yr 0 信誉分
    2024-10-29T07:13:41.83+00:00

    我知道怎么处理了

    我根据https://learn.microsoft.com/zh-cn/previous-versions/office/office-365-api/api/version-2.0/task-rest-operations 这里的提示,设置

    if (check)
    {
        //obj.Add("completedDateTime", new JObject()
        //{
        //    { "timeZone", "UTC" },
        //    { "dateTime", BuildTime(DateTime.UtcNow) }
        //});
        obj.Add("status", "completed");
    }
    else
    {
        //obj.Add("completedDateTime", null);
        obj.Add("status", "inProgress");
    }
    

    即可实现

    0 个注释 无注释

  3. Color_yr 0 信誉分
    2024-10-29T07:15:05.13+00:00
    0 个注释 无注释

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。