Skip to content

Commit 69cf63c

Browse files
Merge pull request #259877 from jfggdl/dec-2023
Updated documentation for creating a renewing a Microsoft Graph API subscription to be used with Event Grid
2 parents d32893f + 1f1a59c commit 69cf63c

9 files changed

+386
-61
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Azure CLI - Create Graph API subscription to subscribe to Microsoft Graph API events using Event Grid partner topics as a notification destination.
3+
description: This article provides a sample Azure CLI script that shows how to create a Microsoft Graph API subscription to receive events via Azure Event Grid partner topics.
4+
ms.devlang: azurecli
5+
ms.topic: sample
6+
ms.date: 12/08/2023
7+
---
8+
9+
```azurecli
10+
mgc subscriptions create --body '{\
11+
"changeType": "updated,deleted,created",\
12+
"notificationUrl": "EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=youPartnerTopic&location=theNameOfAzureRegionFortheTopic",\
13+
"lifecycleNotificationUrl": "EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=yourPartnerTopic&location=theNameOfAzureRegionFortheTopic",\
14+
"resource": "users",\
15+
"expirationDateTime":"2024-03-31T18:23:45.9356913Z",\
16+
"clientState": "secretClientValue"\
17+
}\
18+
'
19+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: Csharp - Create Graph API subscription to subscribe to Microsoft Graph API events using Event Grid partner topics as a notification destination.
3+
description: This article provides a sample C# code that shows how to create a Microsoft Graph API subscription to receive events via Azure Event Grid partner topics.
4+
ms.devlang: csharp
5+
ms.topic: sample
6+
ms.date: 12/08/2023
7+
---
8+
9+
```csharp
10+
// Code snippets are only available for the latest version. Current version is 5.x
11+
12+
// Dependencies
13+
using Microsoft.Graph.Models;
14+
15+
var requestBody = new Subscription
16+
{
17+
ChangeType = "updated,deleted,created",
18+
NotificationUrl = "EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=youPartnerTopic&location=theNameOfAzureRegionFortheTopic",
19+
LifecycleNotificationUrl = "EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=yourPartnerTopic&location=theNameOfAzureRegionFortheTopic",
20+
Resource = "users",
21+
ExpirationDateTime = DateTimeOffset.Parse("2024-03-31T18:23:45.9356913Z"),
22+
ClientState = "secretClientValue",
23+
};
24+
25+
// To initialize your graphClient, see `https://learn.microsoft.com/graph/sdks/create-client?from=snippets&tabs=csharp`
26+
var result = await graphClient.Subscriptions.PostAsync(requestBody);
27+
```
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: Go - Create Graph API subscription to subscribe to Microsoft Graph API events using Event Grid partner topics as a notification destination.
3+
description: This article provides a sample Golang code that shows how to create a Microsoft Graph API subscription to receive events via Azure Event Grid partner topics.
4+
ms.devlang: golang
5+
ms.topic: sample
6+
ms.date: 12/08/2023
7+
---
8+
```go
9+
import (
10+
"context"
11+
"time"
12+
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
13+
graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models"
14+
//other-imports
15+
)
16+
17+
graphClient := msgraphsdk.NewGraphServiceClientWithCredentials(cred, scopes)
18+
19+
20+
requestBody := graphmodels.NewSubscription()
21+
changeType := "updated,deleted,created"
22+
requestBody.SetChangeType(&changeType)
23+
notificationUrl := "EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=yourPartnerTopic&location=theNameOfAzureRegionFortheTopic"
24+
requestBody.SetNotificationUrl(&notificationUrl)
25+
lifecycleNotificationUrl := "EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=yourPartnerTopic&location=theNameOfAzureRegionFortheTopic"
26+
requestBody.SetLifecycleNotificationUrl(&lifecycleNotificationUrl)
27+
resource := "users"
28+
requestBody.SetResource(&resource)
29+
expirationDateTime , err := time.Parse(time.RFC3339, "2024-03-31T18:23:45.9356913Z")
30+
requestBody.SetExpirationDateTime(&expirationDateTime)
31+
clientState := "secretClientValue"
32+
requestBody.SetClientState(&clientState)
33+
34+
subscriptions, err := graphClient.Subscriptions().Post(context.Background(), requestBody, nil)
35+
```
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: Java - Create Graph API subscription to subscribe to Microsoft Graph API events using Event Grid partner topics as a notification destination.
3+
description: This article provides a sample Java code that shows how to create a Microsoft Graph API subscription to receive events via Azure Event Grid partner topics.
4+
ms.devlang: java
5+
ms.topic: sample
6+
ms.date: 12/08/2023
7+
---
8+
9+
```java
10+
GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();
11+
12+
Subscription subscription = new Subscription();
13+
subscription.changeType = "updated,deleted,created";
14+
subscription.notificationUrl = "EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=yourPartnerTopic&location=theNameOfAzureRegionFortheTopic";
15+
subscription.lifecycleNotificationUrl = "EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=yourPartnerTopic&location=theNameOfAzureRegionFortheTopic";
16+
subscription.resource = "users";
17+
subscription.expirationDateTime = OffsetDateTimeSerializer.deserialize("2024-03-31T18:23:45.9356913Z");
18+
subscription.clientState = "secretClientValue";
19+
20+
graphClient.subscriptions()
21+
.buildRequest()
22+
.post(subscription);
23+
24+
```
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: JavaScript - Create Graph API subscription to subscribe to Microsoft Graph API events using Event Grid partner topics as a notification destination.
3+
description: This article provides a sample JavaScript code that shows how to create a Microsoft Graph API subscription to receive events via Azure Event Grid partner topics.
4+
ms.devlang: javascript
5+
ms.topic: sample
6+
ms.date: 12/08/2023
7+
---
8+
9+
```javascript
10+
const options = {
11+
authProvider,
12+
};
13+
14+
const client = Client.init(options);
15+
16+
const subscription = {
17+
changeType: 'updated,deleted,created',
18+
notificationUrl: 'EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=yourPartnerTopic&location=theNameOfAzureRegionFortheTopic',
19+
lifecycleNotificationUrl: 'EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=yourPartnerTopic&location=theNameOfAzureRegionFortheTopic',
20+
resource: 'users',
21+
expirationDateTime: '2024-03-31T18:23:45.9356913Z',
22+
clientState: 'secretClientValue'
23+
};
24+
25+
await client.api('/subscriptions')
26+
.post(subscription);
27+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: PHP - Create Graph API subscription to subscribe to Microsoft Graph API events using Event Grid partner topics as a notification destination.
3+
description: This article provides a sample PHP code that shows how to create a Microsoft Graph API subscription to receive events via Azure Event Grid partner topics.
4+
ms.devlang: php
5+
ms.topic: sample
6+
ms.date: 12/08/2023
7+
---
8+
9+
```php
10+
<?php
11+
12+
// THIS SNIPPET IS A PREVIEW VERSION OF THE SDK. NON-PRODUCTION USE ONLY
13+
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
14+
15+
$requestBody = new Subscription();
16+
$requestBody->setChangeType('updated,deleted,created');
17+
$requestBody->setNotificationUrl('EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=yourPartnerTopic&location=theNameOfAzureRegionFortheTopic');
18+
$requestBody->setLifecycleNotificationUrl('EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=yourPartnerTopic&location=theNameOfAzureRegionFortheTopic');
19+
$requestBody->setResource('users');
20+
$requestBody->setExpirationDateTime(new \DateTime('2024-03-31T18:23:45.9356913Z'));
21+
$requestBody->setClientState('secretClientValue');
22+
23+
$result = $graphServiceClient->subscriptions()->post($requestBody)->wait();
24+
25+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
title: PowerShell - Create Graph API subscription to subscribe to Microsoft Graph API events using Event Grid partner topics as a notification destination.
3+
description: This article provides a sample Azure PowerShell script that shows how to create a Microsoft Graph API subscription to receive events via Azure Event Grid partner topics.
4+
ms.devlang: powershell
5+
ms.topic: sample
6+
ms.date: 12/08/2023
7+
---
8+
9+
```azurepowershell
10+
Import-Module Microsoft.Graph.ChangeNotifications
11+
12+
$params = @{
13+
changeType = "updated,deleted,created"
14+
notificationUrl = "EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=yourPartnerTopic&location=theNameOfAzureRegionFortheTopic"
15+
lifecycleNotificationUrl = "EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=yourPartnerTopic&location=theNameOfAzureRegionFortheTopic"
16+
resource = "users"
17+
expirationDateTime = [System.DateTime]::Parse("2024-03-31T18:23:45.9356913Z")
18+
clientState = "secretClientValue"
19+
}
20+
21+
New-MgSubscription -BodyParameter $params
22+
23+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Python - Create Graph API subscription to subscribe to Microsoft Graph API events using Event Grid partner topics as a notification destination.
3+
description: This article provides a sample Python code that shows how to create a Microsoft Graph API subscription to receive events via Azure Event Grid partner topics.
4+
ms.devlang: python
5+
ms.topic: sample
6+
ms.date: 12/08/2023
7+
---
8+
9+
```python
10+
graph_client = GraphServiceClient(credentials, scopes)
11+
12+
request_body = Subscription(
13+
change_type = "updated,deleted,created",
14+
notification_url = "EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=yourPartnerTopic&location=theNameOfAzureRegionFortheTopic",
15+
lifecycle_notification_url = "EventGrid:?azuresubscriptionid=8A8A8A8A-4B4B-4C4C-4D4D-12E12E12E12E&resourcegroup=yourResourceGroup&partnertopic=yourPartnerTopic&location=theNameOfAzureRegionFortheTopic",
16+
resource = "users",
17+
expiration_date_time = "2024-03-31T18:23:45.9356913Z",
18+
client_state = "secretClientValue"
19+
)
20+
21+
result = await graph_client.subscriptions.post(request_body)
22+
```

0 commit comments

Comments
 (0)