0

I have a plannerTask and in its Details it has a CheckList. I use it to programatically insert CheckListItems in it, and it all works like a charm when inserting or retrieving the tasks.

My problem arrives when I am going to insert a new CheckListItem and the CheckList already has 20 items. It returns a MaximumChecklistItemsOnTask (because it is forbidden to insert more than 20 items in a check list).

Solution could be to remove the oldest item, but I am not able to do it. I have tried this:

var elementToRemove = oldDetails.Checklist.Where(c => c.Value.IsChecked).OrderBy(c => c.Value.LastModifiedDateTime).First();
oldDetails.Checklist = oldDetails.Checklist.Where(c => c.Value.LastModifiedDateTime <> elementToRemove.Value.LastModifiedDateTime);

But it throws a casting error in the second line:

Unable to cast object of type 'WhereEnumerableIterator1[System.Collections.Generic.KeyValuePair2[System.String,Microsoft.Graph.PlannerChecklistItem]]' to type 'Microsoft.Graph.PlannerChecklistItems'.

Which is the right way to remove the oldest element from the ChecklistItem?

UPDATE: In first place I retrieve a plannerTask from the server. Then I get the details from this plannerTask. So oldDetails is a plannertaskdetails object (https://learn.microsoft.com/en-us/graph/api/resources/plannertaskdetails?view=graph-rest-1.0). Inside the plannertaskdetails object (oldDetails), I have the plannerchecklistitems object (oldDetails.Checklist): https://learn.microsoft.com/en-us/graph/api/resources/plannerchecklistitems?view=graph-rest-1.0. If plannerchecklistitems were just a List, it would be as easy as list.Remove(item), but it is not a normal list, and that is why I am not able to remove the item.

UPDATE 2: I have found this way to remove the item from oldDetails:

oldDetails.Checklist.AdditionalData.Remove(elementToRemove.Key)

But, the the way I send the changes to the server is this:

await graphClient.Planner.Tasks(plannerTask.Id).Details.Request().Header("If-Match", oldDetails.GetEtag).UpdateAsync(newDetails)

As it is a PATCH request (not a PUT one), I only have in newDetails the records that have changed, it is, the new records. How could I specify there that a record has been deleted from the list? Sorry if my English is not good enough to express myself properly, but what I mean is that newDetails is not the full list, it only contains the records that must be added and I do not know how to specify in that request that one record must be deleted.

Carlos
  • 1,638
  • 5
  • 21
  • 39
  • Looks like you are trying to do it server-side (i.e. transform your linq into a query)? Maybe you can just get the items first to the client and then remove like you did. – Nikolay Nov 19 '21 at 22:18
  • @Nikolay I was trying to do it in the client. In fact, I have in the client the plannertaskdetails (oldDetails), and the plannerChecklistItems (oldDetails.Checklist). The problem is that it has not a "removeItem" method or similar, that is why I try to "filter" in this way. – Carlos Nov 19 '21 at 23:02

0 Answers0