Filter data from Microsoft graph

Eduardo Gomez 4,156 Reputation points
2025-04-26T16:19:58.76+00:00

I have all my in a list

public partial class CalendarPageViewModel(GraphServiceClient graphClient) : ObservableObject {
    public ObservableCollection<Event> CalendarEvents { get; set; } = [];
    public async Task FetchCalendarEventsAsync() {
        var graphEventsResponse = await graphClient.Me.Events.GetAsync();
        if(graphEventsResponse != null) {
            foreach(var items in graphEventsResponse?.Value!) {
                CalendarEvents.Add(items);
            }
        }


But that is giving me all the event from the calendar, but I don't care about past events, I want to get the events from today going forward

I tried this code

        var today = DateTime.UtcNow.ToString("o"); 
        var graphEventsResponse = await graphClient.Me.Events.GetAsync(requestConfiguration =>
        {
            requestConfiguration.QueryParameters.Filter = $"start/DateTime ge {today}";
            requestConfiguration.QueryParameters.Orderby = new List<string> { "start/DateTime asc" };
        });
        if (graphEventsResponse != null)
        {
            CalendarEvents.Clear();
            foreach (var item in graphEventsResponse.Value!)
            {
                CalendarEvents.Add(item);
            }
        }


and I get the error of debugger is not attach to this project error

Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 61,111 Reputation points
    2025-04-27T02:42:30.16+00:00

    Your filter parameter is incorrect. The comparison should be against a string value in quotes. Try something like this.

    var today = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss");
    
    var graphEventsResponse = await graphClient.Me.Events.GetAsync(config => {
        config.QueryParameters.Filter = $"start/DateTime ge '{today}'";
        config.QueryParameters.Orderby = new[] { "start/DateTime asc" };
    });
    
    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.