Search code examples
pythonmicrosoft-graph-api

Graph API filter calendar and room request?


I work with the new Microsoft Graph API python sdk and want to retrieve room lists and calendar events related to them. I managed to get a list of rooms and related calendar events but I would like to do some filtering on those, essentially:

  1. Find rooms only on a certain floor
  2. Find rooms only wheelchairs accessible
  3. Find rooms containing a certain keyword in the name
  4. Get all booked events for those rooms within a start and stop datetime.

Any idea how I can add filtering to the calls?

I use: ´graph_client.places.graph_room.get()´ to get the rooms and their associated emails. ´graph_client.users.by_user_id(room_mail).calendar.events.get()´ to get calendar events.

But it is unclear how to filter using those (and documentation for python is basically inexistant from what I saw so far)

Here is the code so far:

    #!/usr/bin/env python3
    
    import asyncio
    from msgraph.generated.models.room import Room
    from msgraph.generated.users.item.calendar.get_schedule.get_schedule_request_builder import GetScheduleRequestBuilder
    from msgraph.generated.users.item.calendar.get_schedule.get_schedule_post_request_body import GetSchedulePostRequestBody
    from msgraph import GraphServiceClient
    
    from azure.identity import DeviceCodeCredential, CertificateCredential, AuthorizationCodeCredential

    config = {
      'client_id': '<something>',
      'tenant_id': '<something>',
      'client_secret': '<something>',
      'authority': 'https://login.microsoftonline.com/<something>',
      'scope': ['https://graph.microsoft.com/.default'],
      'certificate_path': 'msal_crt_n_key.pem',
    }
    async def get_schedule_for_room(graph_client, mail):
    result = await graph_client.users.by_user_id(mail).calendar.events.get()
    return result

    async def get_rooms(graph_client):
        return await graph_client.places.graph_room.get()
    
    async def main():
        # azure.identity.aio
        print("Login using certificate")
        credential = CertificateCredential(
            tenant_id=config['tenant_id'],
            client_id=config['client_id'],
            certificate_path=config['certificate_path'])
    
        graph_client = GraphServiceClient(credential, config['scope']) # type: ignore
        print("Get list of rooms")
        rooms = await get_rooms(graph_client)
        print(f"Rooms: {rooms}")
    
        room_emails = [r.email_address for r in rooms.value]
        print(f"Rooms emails: {room_emails}")
    
        for mail in room_emails:
            calendar = await get_schedule_for_room(graph_client, mail)
            print(calendar)
            print(type(calendar))
    
    asyncio.run(main())


Solution

    1. Find rooms only on a certain floor
    • Query
    https://graph.microsoft.com/v1.0/places/microsoft.graph.room?$filter=floorNumber eq 2
    
    • Code snippet for the Python SDK
    query_params = GraphRoomRequestBuilder.GraphRoomRequestBuilderGetQueryParameters(
                filter = "floorNumber eq 2",
        )
        
    request_configuration = GraphRoomRequestBuilder.GraphRoomRequestBuilderGetRequestConfiguration(
        query_parameters = query_params,
        )
        
    result = await client.places.graph_room.get(request_configuration = request_configuration)
    
    1. Find rooms only wheelchairs accessible
    • Query
    https://graph.microsoft.com/v1.0/places/microsoft.graph.room?$filter=isWheelChairAccessible eq true
    
    • Code snippet for the Python SDK
    query_params = GraphRoomRequestBuilder.GraphRoomRequestBuilderGetQueryParameters(
            filter = "isWheelChairAccessible eq true",
    )
    
    request_configuration = GraphRoomRequestBuilder.GraphRoomRequestBuilderGetRequestConfiguration(
            query_parameters = query_params,
    )
    
    result = await client.places.graph_room.get(request_configuration = request_configuration)
    
    1. Find rooms containing a certain keyword in the name
    • Query
    https://graph.microsoft.com/v1.0/places/microsoft.graph.room?$filter=contains(displayName,'xxx')
    
    • Code snippet for the Python SDK
    query_params = GraphRoomRequestBuilder.GraphRoomRequestBuilderGetQueryParameters(
            filter = "contains(displayName,'xxx')",
    )
    
    request_configuration = GraphRoomRequestBuilder.GraphRoomRequestBuilderGetRequestConfiguration(
    query_parameters = query_params,
    )
    
    
    result = await client.places.graph_room.get(request_configuration = request_configuration)
    

    4.Get all booked events for those rooms within a start and stop datetime.

    • Query I would use calenderView endpoint where you can specify start and end date time
    https://graph.microsoft.com/v1.0/users/room_email_address/calendarView?startDateTime=2023-08-25T00:00:00Z&endDateTime=2023-08-31T00:00:00Z
    
    • Code snippet for the Python SDK
    query_params = CalendarViewRequestBuilder.CalendarViewRequestBuilderGetQueryParameters(
            startDateTime = "2023-08-25T00:00:00Z",
            endDateTime = "2023-08-31T00:00:00Z",
    )
    
    request_configuration = CalendarViewRequestBuilder.CalendarViewRequestBuilderGetRequestConfiguration(
    query_parameters = query_params,
    )
    
    
    result = await client.users.by_user_id('room_email_address').calendar_view.get(request_configuration = request_configuration)
    

    Use these imports

    from msgraph.generated.places.graph_room.graph_room_request_builder import GraphRoomRequestBuilder
    
    from msgraph.generated.users.item.calendar.calendar_view.calendar_view_request_builder import CalendarViewRequestBuilder