Leonardo Heis
Leonardo Heis

Reputation: 109

Retrieve Task Count from Plans

I'm currently using Microsoft Graph to interface with Planner in order to create tasks with specific information. I'm using an Angular and WebApi 2/ASP.NET 5 solutions.

Right now, when I'm trying to consolidate the quantity of Tasks per Plan I have in the DB against I have within Planner's Plans, the retrieved information takes too much time to return to my webapi controller (almost 39 seconds to retrieve 205 plans with the count of theirs tasks). Also is imperative to add a retry attempt policy that I'm achieving using an external library (Polly Library)

For each Plan I have in the DB I'm going to Microsoft Graph API to retrieve all the task It has. If I do not use the retry policy, in many opportunities, I lose some tasks from many plans, I don't know why.

public async Task<int> GetTasksCountInPlan(GraphServiceClient graphClient, string planId)
{
    var errors = new Dictionary<string, string>();

    try
    {
        var retryPolicy = Policy
            .Handle<ServiceException>()
            .Or<Exception>()
            .WaitAndRetryAsync(2, retryAttempt => TimeSpan.FromMilliseconds(10));

        var task = await retryPolicy.ExecuteAsync(() =>
            graphClient.Planner
            .Plans[planId]
            .Tasks
            .Request()
            .GetAsync());

        return task?.Count ?? 0;
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine($"Task Count Error for PlanId: {planId} Etag:{ex.Message}");
        errors.Add(planId, ex.Message);
        return 0;
    }
}

The code I'm describing above is iterated for each plan I have in the DB.

Is there any possibility to access to these tasks of each plan in order to obtain the counting and not all the description and also is there a way to retrieve the information I need using a bulk of plans and not iterating over each plan?

Upvotes: 1

Views: 821

Answers (1)

Marc LaFleur
Marc LaFleur

Reputation: 33124

You can get the number of task items by passing the Count query parameter ($count=true). This gets passed to the Request() method as a List<QueryOption>:

var queryOptions = new List<QueryOption>() { new QueryOption("$count", "true") };

var tasks = await retryPolicy.ExecuteAsync(() =>
    graphClient.Planner
    .Plans[planId]
    .Tasks
    .Request()
    .GetAsync());

return tasks.AdditionalData["@odata.count"]

Upvotes: 2

Related Questions