skip to Main Content

I have following PowerShell script which add event on the Office 365 group calendar. When I run the following script, it will only add the first event but not the second one. And there is no error. Have I missed anything on the script? Any help on this would be much appreciated, thank you.

$json = @"
  {
    "subject": "Event1",
    "start": {
      "timeZone": "UTC",
      "dateTime": "2023-06-10T08:00"
    },
    "end": {
      "timeZone": "UTC",
      "dateTime": "2023-06-10T09:00"
    }
  },
  {
    "subject": "Event2",
    "start": {
      "timeZone": "UTC",
      "dateTime": "2023-06-11T08:00"
    },
    "end": {
      "timeZone": "UTC",
      "dateTime": "2023-06-11T09:00"
    }
  }
"@
$apiUrl = "https://graph.microsoft.com/v1.0/groups/xxxx-xxxx-xxxx-xxx/events"
Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri $apiUrl -Method Post -ContentType "application/json" -Body $json

3

Answers


  1. Chosen as BEST ANSWER

    I am getting following error

    Response status code does not indicate success: 405 (Method Not Allowed).
    

    Same json works fine from https://developer.microsoft.com/en-us/graph/graph-explorer

    $json = @"
    {
        "requests": [
            {
                "url": "/groups/xxx/events",
                "method": "POST",
                "id": "1",
                "body": {
                    "subject": "Event1",
                    "start": {
                        "timeZone": "UTC",
                        "dateTime": "2023-06-10T08:00"
                    },
                    "end": {
                        "timeZone": "UTC",
                        "dateTime": "2023-06-10T09:00"
                    }
                },
                "headers": {
                    "Content-Type": "application/json"
                }
            },
            {
                "url": "/groups/xxx/events",
                "method": "POST",
                "id": "2",
                "body": {
                    "subject": "Event2",
                    "start": {
                        "timeZone": "UTC",
                        "dateTime": "2023-06-10T08:00"
                    },
                    "end": {
                        "timeZone": "UTC",
                        "dateTime": "2023-06-10T09:00"
                    }
                },
                "headers": {
                    "Content-Type": "application/json"
                }
            }
        ]
    }
    "@
    $apiUrl = "https://graph.microsoft.com/v1.0/$batch"
    Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri $apiUrl -Method Post -ContentType "application/json" -Body $json
    

  2. You cannot use the Graph API to create multiple events at once, to create multiple events you have to use batching, see this link for more information:

    See an example here:

    {  
      "requests": [  
        {  
          "id": "1",  
          "method": "POST",  
          "url": "/groups/{id}/calendar/events",  
          "body": { "event 1 body here" },  
          "headers": {  
            "Content-Type": "application/json"  
          }  
        },  
        {  
          "id": "2",  
          "method": "POST",  
          "url": "/groups/{id}/calendar/events",  
          "body": {"event 2 body here"},  
          "headers": {  
            "Content-Type": "application/json"  
          }  
        }
      ]  
    }  
    
    Login or Signup to reply.
  3. The endpoint https://graph.microsoft.com/v1.0/groups/xxxx-xxxx-xxxx-xxx/events allows to add only one event. So the second event is ignored.

    Use batch endpoint

    $json = @"
    {
        "requests": [
            {
                "url": "/groups/xxx/events",
                "method": "POST",
                "id": "1",
                "body": {
                    "subject": "Event1",
                    "start": {
                    "timeZone": "UTC",
                    "dateTime": "2023-06-10T08:00"
                    },
                    "end": {
                    "timeZone": "UTC",
                    "dateTime": "2023-06-10T09:00"
                    }
                },
                "headers": {
                    "Content-Type": "application/json"
                }
            },
            {
                "url": "/groups/xxx/events",
                "method": "POST",
                "id": "1",
                "body": {
                    "subject": "Event2",
                    "start": {
                    "timeZone": "UTC",
                    "dateTime": "2023-06-11T08:00"
                    },
                    "end": {
                    "timeZone": "UTC",
                    "dateTime": "2023-06-11T09:00"
                    }
                },
                "headers": {
                    "Content-Type": "application/json"
                }
            }
        ]
    }
    "@
    $apiUrl = "https://graph.microsoft.com/v1.0/`$batch"
    Invoke-RestMethod -Headers @{Authorization = "Bearer $($Tokenresponse.access_token)"} -Uri $apiUrl -Method Post -ContentType "application/json" -Body $json
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search