pythonazuremicrosoft-graph-api

MS Graph - List events (read) - Recurring events


I would like to ask you for an help related to the reading my callendar events from MS Exchange by MS Graph API.

Issue: My code do not "download" the recurring Events from calendar, all others are downloaded.

Note:

  1. Im successfully authenticated OAuth2 --> so this code is not listed here
  2. Code it self is called from another cade so variables are hand over. I use this for testing:

My code:

# Update filters)
Filter_Start_Date = Filter_Start_Date + "T00:00:00Z"
Filter_End_Date = Filter_End_Date + "T00:00:00Z"

headers = {
    "Authorization": f"Bearer {access_token}",
    "Content-Type": "application/json",
    "Prefer": 'outlook.timezone="Europe/Paris"'}

params = {
    "$filter": f"start/dateTime ge \'{Filter_Start_Date}\' and end/dateTime le \'{Filter_End_Date}\'",  #! To be done --> maybe here is some issue with filtering data !!!!
    "$orderby": f"start/dateTime desc",
    "$top": 1000,
    "$count": "true",
    "$select": "subject, start, end, categories, recurrence, showAs, location, isAllDay, bodyPreview"}

Events_downloaded = {}
events_url = f"https://graph.microsoft.com/v1.0/users/{username}/events"
events_response = requests.get(url=events_url, headers=headers, params=params)

Counter = 0 
if response.status_code == 200:
    # Init page 
    Events = events_response.json()

Can anyone tell me what do I do wrong?

Thank you

Jan Vaško


Solution

  • For sample, I created a recurring event using Microsoft Graph API:

    enter image description here

    To read the recurring events make use of below code:

    access_token = "AccessToken"
    username = "user@xxx.onmicrosoft.com"  
    
    # Define start and end date for filtering events (adjust these dates based on your needs)
    Filter_Start_Date = "2024-12-12T00:00:00Z"
    Filter_End_Date = "2024-12-31T23:59:59Z"  
    
    headers = {
        "Authorization": f"Bearer {access_token}",
        "Content-Type": "application/json",
        "Prefer": 'outlook.timezone="Europe/Paris"'  
    }
    
    params = {
        "startDateTime": Filter_Start_Date,
        "endDateTime": Filter_End_Date,
        "$orderby": "start/dateTime",
        "$top": 1000,  
        "$select": "id,subject,start,end,location,recurrence,seriesMasterId"
    }
    
    # Endpoint to fetch all events (including recurrence details)
    events_url = f"https://graph.microsoft.com/v1.0/users/{username}/events"
    
    response = requests.get(events_url, headers=headers, params=params)
    
    if response.status_code == 200:
        events = response.json()
        print("Fetched Events:")
        for event in events["value"]:
            print(f"Subject: {event['subject']}")
            print(f"Start: {event['start']['dateTime']}")
            print(f"End: {event['end']['dateTime']}")
            print(f"Location: {event['location']['displayName']}")
            # Recurrence might not be present for occurrences, so check for it on the seriesMasterId
            if "recurrence" in event:
                print(f"Recurrence: {event['recurrence']}")
            elif "seriesMasterId" in event:
                print(f"Event is part of a recurring series (Series ID): {event['seriesMasterId']}")
            else:
                print("Recurrence: None")
            print("--------------------------------------------------")
    else:
        print(f"Error: {response.status_code}")
        print(response.json())
    

    enter image description here

    If still the issue persists, check the below: