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 'WhereEnumerableIterator
1[System.Collections.Generic.KeyValuePair
2[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.