microsoft-graph-apimicrosoft-graph-calendar

How-To list already started calender eventrs using graph api(http request)


I have a PowerShell script to list the calendar events of a specific user from the time the script is executed up to a week. I use the following variables to get the timezone, start- & enddates and start- enddtime. I also obtain an accesstoken from graph:

    # Get current time zone
    $TimeZone = (Get-TimeZone).Id 
    
    # Filter - the below query returns events for that day
    $startDate = (Get-Date -UFormat '+%Y-%m-%dT%H:%M:%S.000Z')
    # Enddate is always always one week. Enter a value in the brackets next to AddDays to move the end date on a per-day basis.
    $endDate = ((Get-Date).AddDays(7) |Get-Date -UFormat '+%Y-%m-%dT23:59:59.000Z')

   ## Obtaining access token
            
   # Using Microsoft Authentication Library  to obtain a access token with delegated permission
   $AccessToken = (Get-MsalToken @MsalParams).AccessToken
                                                                                 

After this i use the following to form a header to add to the invoke-restmethode http request using powershell.

    ## getting calender entries 
     
    #Form request headers with the acquired $accessToken
    $headers = @{}
    $headers = @{'Content-Type'="application\json";'Authorization'="Bearer $AccessToken"}
     
    # Set a time zone in header to get date time values returned in the specific time zone
    $headers.Add('Prefer', 'outlook.timezone="' + $TimeZone + '"')
    

Then I prepare a http link to use in the invoke-restmethode PS command and put it into an variable:

#   Generating link to make the https request on graph to obtain the calender entries from exchange server of the specific user
#   Applying filter with meeting start time to get events
$apiUrl = "https://graph.microsoft.com/v1.0/users/USERID/calendarview?startdatetime=$startDate&enddatetime=$endDate"
        

This variable will has a value of

https://graph.microsoft.com/v1.0/users/USERID/calendarview?startdatetime=2022-12-01T15:05:46.000Z&enddatetime=2022-12-08T23:59:59.000Z

Finally, i call the invoke-restmethod with the prepared link and the header with timezone information:

$Response = Invoke-RestMethod -Method Get -Uri $apiUrl -Headers $headers -ErrorAction SilentlyContinue

The problem here is, this request gives only events that are starting after the startdatetime (2022-12-01T15:05:46.000Z) and those events that have already started but aren't going to end in the next hour, even though i give my timezone offset into the header using the $TimeZone. If I have events that has already started and still going on but going to end within an hour, those won't be listed in the result.

My question to the experts is: How can I list all calendar events that have already started and are still on going when the script is executed, as well as events that will start from the time the script is executed up to a week. An Excample is: the specific user had the following 3 events:

Event 01 : Start 01.12.2022 13:30 End 01.12.2022 17:48

Event 02 : Start 01.12.2022 15:30 End 01.12.2022 18:00

Event 03 : Start 01.12.2022 18:00 End 01.12.2022 22:00

He executes the script on 01.12.2022 at 16:30. The script gives in this case the events 02 & 03 but not the event 01. The reason seems tobe this event is going to end within the next one hour. I want to list this event 01 aswell.


Solution

  • You are specifying startdatetime and enddatetime without timezone offset. If no timezone offset is included in the value, it is interpreted as UTC.

    startdatetime=2022-12-01T15:05:46.000Z&enddatetime=2022-12-08T23:59:59.000Z
    

    If the user executes the script at 15:05:46 his local time and this time is used in the startdatetime, value then Graph API interprets the time as UTC.

    In PowerShell I would use

    (Get-Date).ToUniversalTime().ToString("o")
    

    or to include timezone offset

    (Get-Date).ToString("o")