Share via

How to create multiple subscription for a user resource to get change notification

Agrawal, Amit 20 Reputation points
Sep 4, 2024, 2:42 PM

Is it possible to create multiple subscription for a user resource to get change notification using Graph API (https://graph.microsoft.com/v1.0/) . Each subscription may or may not have same notification URL but resource and changetype is defiantly same for a Entra tenant.

As per microsoft link https://learn.microsoft.com/en-us/graph/api/subscription-post-subscriptions?view=graph-rest-1.0&tabs=go , it seems if resource and changetype is same for subscription payload then it is considered Duplicate subscription.
kindly advise how to achieve multiple subscription for same resource & changetype combination

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
24,133 questions
0 comments No comments
{count} votes

Accepted answer
  1. James Hamil 27,186 Reputation points Microsoft Employee
    Sep 9, 2024, 8:16 PM

    Hi @Agrawal, Amit , yes this should be possible! However, as you mentioned, if the resource and change type are the same for a subscription payload, it is considered a duplicate subscription. You can avoid this by adding a custom header to each subscription request with a unique identifier for that subscription. This way even if the resource and change type are the same, the subscription will be considered unique because of the custom header.

    Here is an example of how to do this:

    import (
        "net/http"
        "fmt"
        "bytes"
    )
    
    func createSubscription() {
        url := "https://graph.microsoft.com/v1.0/subscriptions"
    
        // Define the subscription payload
        subscription := map[string]interface{}{
            "changeType": "created",
            "notificationUrl": "https://myapp.azurewebsites.net/api/notifications",
            "resource": "/me/mailfolders('inbox')/messages",
            "expirationDateTime": "2022-01-01T11:23:45.6789012Z",
        }
    
        // Convert the subscription payload to JSON
        jsonPayload, err := json.Marshal(subscription)
        if err != nil {
            fmt.Println(err)
            return
        }
    
        // Create a new HTTP request with the subscription payload
        req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
        if err != nil {
            fmt.Println(err)
            return
        }
    
        // Add a custom header to the request with a unique identifier for the subscription
        req.Header.Set("My-Subscription-Id", "123456")
    
        // Send the request and handle the response
        client := &http.Client{}
        resp, err := client.Do(req)
        if err != nil {
            fmt.Println(err)
            return
        }
        defer resp.Body.Close()
    
        // Handle the response
        if resp.StatusCode == http.StatusCreated {
            fmt.Println("Subscription created successfully")
        } else {
            fmt.Println("Error creating subscription")
        }
    }
    

    The custom header "My-Subscription-Id" is added to the subscription request with a unique identifier for the subscription. This way, even if the resource and change type are the same for multiple subscriptions, each subscription will be considered unique because of the custom header.

    Please let me know if you have any questions and I can help you further.

    If this answer helps you please mark "Accept Answer" so other users can reference it.

    Thank you,

    James


1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    Sep 4, 2024, 2:42 PM
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Yes, it is possible to create multiple subscriptions for a user resource to get change notifications using the Microsoft Graph API. Each subscription can have a different notification URL, but the resource and change type must be the same for a given tenant. To create a subscription, send a POST request to the /subscriptions endpoint with the desired parameters, including the resource URL, change type, and notification URL. You can create up to 1000 active subscriptions for Outlook resources per mailbox for all applications.

    References:

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.