Search code examples
.netodatamicrosoft-teamsmicrosoft-graph-teams

Odata filter DateTime casting string to DateTime and Filtering


I am trying to retrieve all records (webinars) after the specific date as follows:

var result = await graphClient.Solutions.VirtualEvents.Webinars
.GetByUserIdAndRoleWithUserIdWithRole("organizer", "16b1190f-0552-4c95-90f5-c4d2d852812b").GetAsync((requestConfiguration) =>
{
    requestConfiguration.QueryParameters.Count = true;
    // requestConfiguration.QueryParameters.Filter =
    //     "date(startDateTime/dateTime) ge 2024-03-26T12:00:00Z";
    requestConfiguration.QueryParameters.Filter =
        "cast(startDateTime/dateTime, Edm.DateTimeOffset) ge 2024-03-26T12:00:00Z";
});

The type of the Webinar model StartDateTime.DateTime is a string: enter image description here

I've tried different approaches but either I get "Edm.String" and "Edm.DataTimeOffset" error or the filter does not work enter image description here


Solution

  • I've never tried to filter webinars, but for other endpoints where some property type is DateTimeTimeZone, I need to enclose date time in single quotes.

    $filter=start/dateTime ge '2024-02-03T12:00:00'
    

    If you need to include time zone offset, then encode + as %2B

    $filter=start/dateTime ge '2024-02-03T12:00:00%2B2' // +2
    $filter=start/dateTime ge '2024-02-03T12:00:00-2' // -2
    

    I'm not sure whether the Graph SDK will automatically encode + in filter query.

    var result = await graphClient.Solutions.VirtualEvents.Webinars
    .GetByUserIdAndRoleWithUserIdWithRole("organizer", "16b1190f-0552-4c95-90f5-c4d2d852812b").GetAsync((rc) =>
    {
        rc.QueryParameters.Count = true;
        rc.QueryParameters.Filter = "startDateTime/dateTime ge '2024-03-26T12:00:00Z'";
    });