简体   繁体   中英

MS Graph SDK C# - Get all Tasks within a Plan

I am trying to obtain all Tasks (title or whatever) from a certain plan.

Currently, the code I have just returns the plans I am part of or the tasks assigned to me, but I would also like to retrieve the Tasks that are within a {lan that are not assigned.

var plans = await graphServiceClient
    .Me
    .Planner
    .Plans
    .Request()
    .GetAsync();

foreach (var plan in plans)
{
    //works
    Console.WriteLine(plan.Title);
    Console.WriteLine(plan.Id);

    //does not work
    Console.WriteLine(plan.Tasks);
    Console.WriteLine(plan.Tasks.Count);
}

var tasks = await graphServiceClient
    .Me
    .Planner
    .Tasks
    .Request()
    .GetAsync();

foreach (var task in tasks)
{
    Console.WriteLine(task.Title);
}

Withing the part of the code where //does not work are the two lines that do not work (even if used 1 at a time)

Can't think of a way to get the unassigned tasks. Tried to remove the .Me part, but nothing seems to work.

The SDK won't automatically populate plan.Tasks which is why they're showing up as null. You'll need to request them separately.

To do that, you first need to note the difference between getting a list of your plans ( /me/planner/plans/ ), a single plan ( /planner/plans/{id} ) and the tasks within a single plan ( /planner/plans/{id}/tasks ).

So first you need to fetch the list of plans, then retrieve the tasks as you iterate over the plans:

var plans = await graphServiceClient
    .Me
    .Planner
    .Plans
    .Request()
    .GetAsync();

foreach (var plan in plans)
{
    Console.WriteLine($"{plan.Title} ({plan.Id})");

    var tasks = await graphServiceClient
        .Planner
        .Plans[plan.Id]
        .Tasks
        .Request()
        .GetAsync();

    foreach (var task in tasks)
    {
        Console.WriteLine($"{task.Title} ({task.Id})");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM