APIM set-variable error

Wael Horchani-TEN 40 Reputation points
2025-06-02T10:12:16.84+00:00

Hello team,

I'm trying to add a member to a security group using microsoft graph, I'm calling it from APIM.

Below used code in APIM and it throw an error in line 14 (the set variable syntax)

The syntax is generated by Copilot, I totally new to this field. Please help.

User's image


Microsoft Security | Microsoft Entra | Microsoft Entra ID

2 answers

Sort by: Most helpful
  1. Wael Horchani-TEN 40 Reputation points
    2025-06-04T15:01:08.8333333+00:00

    Hi All,

    Finally I setup working code, thank you very much for your help. you can check below full code for reference :

    <policies>

    <inbound>

    <base />

    <!-- Step 1: Initialize Group Name and User Email -->

    <set-variable name="groupName" value="@(context.Request.OriginalUrl.Query.GetValueOrDefault("group_name"))" />

    <!-- set-variable name="userEmail" value="@{

    var userEmailValue = context.Request.Url.Query.GetValueOrDefault("user_email", "");

    return userEmailValue;

    }" / -->

    <set-variable name="userEmail" value="@(context.Request.OriginalUrl.Query.GetValueOrDefault("user_email"))" />

    <!--set-variable name="groupName" value="@(context.Request.MatchedParameters["group_name"])" />

    <set-variable name="userEmail" value="@(context.Request.MatchedParameters["user_email"])" /-->

    <!-- Step 2: Acquire Microsoft Graph Token -->

    <send-request mode="new" response-variable-name="tokenResponse" timeout="20">

    <set-url>https://login.microsoftonline.com/{tenant-id}/oauth2/v2.0/token</set-url>

    <set-method>POST</set-method>

    <set-header name="Content-Type" exists-action="override">

    <value>application/x-www-form-urlencoded</value>

    </set-header>

    <set-body>@("client_id={client-id}&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret={client-secret}&grant_type=client_credentials")</set-body>

    </send-request>

    <!--Store The Token in "access_token" variable-->

    <set-variable name="access_token" value="@(((IResponse)context.Variables["tokenResponse"]).Body.As<JObject>()["access_token"].ToString())" />

    <!-- Step 3: Get Group ID by Name -->

    <send-request mode="new" response-variable-name="groupResponse" timeout="20">

    <set-url>@("https://graph.microsoft.com/v1.0/groups?$filter=displayName eq '" + context.Variables.GetValueOrDefault<string>("groupName") + "'")</set-url>

    <set-method>GET</set-method>

    <set-header name="Authorization" exists-action="override">

    <value>@("Bearer " + context.Variables["access_token"])</value>

    </set-header>

    </send-request>

    <!--Store The Group ID in "groupId" variable-->

    <set-variable name="groupId" value="@(((IResponse)context.Variables["groupResponse"]).Body.As<JObject>()["value"]?[0]?["id"])" />

    <!-- Step 4: Get User ID by Email -->

    <send-request mode="new" response-variable-name="userResponse" timeout="20">

    <set-url>@("https://graph.microsoft.com/v1.0/users/" + context.Variables.GetValueOrDefault<string>("userEmail"))</set-url>

    <set-method>GET</set-method>

    <set-header name="Authorization" exists-action="override">

    <value>@("Bearer " + context.Variables["access_token"])</value>

    </set-header>

    </send-request>

    <!--Store The User ID in "userId" variable-->

    <set-variable name="userId" value="@(((IResponse)context.Variables["userResponse"]).Body.As<JObject>()?["id"])" />

    <!-- Step 5: Add User to Group-->

    <set-method>POST</set-method>

    <set-header name="Authorization" exists-action="override">

    <value>@("Bearer " + context.Variables["access_token"])</value>

    </set-header>

    <set-header name="Content-Type" exists-action="override">

    <value>application/json</value>

    </set-header>

    <set-body>@("{\"@odata.id\": \"https://graph.microsoft.com/v1.0/directoryObjects/" + context.Variables["userId"] + "\"}")</set-body>

    <rewrite-uri template="@("/v1.0/groups/" + context.Variables["groupId"] + "/members/$ref")" copy-unmatched-params="false" />

    <set-backend-service base-url="https://graph.microsoft.com" />

    </inbound>

    <backend>

    <base />

    </backend>

    <outbound>

    <base />

    <choose>

    <when condition="@(context.Response.StatusCode == 204)">

    <return-response>

    <set-status code="200" reason="OK" />

    <set-body>{ "message": "User Added Successfully." }</set-body>

    </return-response>

    </when>

    <when condition="@(context.Response.StatusCode == 400)">

    <return-response>

    <set-status code="200" reason="OK" />

    <set-body>{ "message": "Bad Request. User Already Member of The Group Or Invalid User Email." }</set-body>

    </return-response>

    </when>

    <when condition="@(context.Response.StatusCode == 500)">

    <return-response>

    <set-status code="200" reason="OK" />

    <set-body>{ "message": "Bad Request. Invalid Group Name." }</set-body>

    </return-response>

    </when>

    </choose>

    </outbound>

    <on-error>

    <base />

    </on-error>

    </policies>

    Was this answer helpful?

    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

Your answer

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