Edit

Upsert resourcePolicyDefinition

Namespace: microsoft.graph

Important

APIs under the /beta version in Microsoft Graph are subject to change. Use of these APIs in production applications is not supported. To determine whether an API is available in v1.0, use the Version selector.

Create a resourcePolicyDefinition if it doesn't exist, or update it if it exists.

The unique name in the request URL identifies the object. Supply the fields needed to create the object if it doesn't exist. An existing object is updated with a 204 No Content response.

This operation accepts JSON or plain-text Rego policies and is intended primarily for declarative deployments, such as Bicep templates.

For policy structure, metadata, parameters, and examples, see Author policies with Rego.

Permissions

Choose the permission or permissions marked as least privileged for this API. Use a higher privileged permission or permissions only if your app requires it. For details about delegated and application permissions, see Permission types. To learn more about these permissions, see the permissions reference.

Permission type Least privileged permissions Higher privileged permissions
Delegated (work or school account) ResourcePolicyDefinition.ReadWrite.All Not available.
Delegated (personal Microsoft account) Not supported. Not supported.
Application ResourcePolicyDefinition.ReadWrite.All Not available.

Important

For delegated access using work or school accounts, the signed-in user must be assigned a supported Microsoft Entra role or a custom role that grants the permissions required for this operation. Resource Policy Administrator is the least privileged role supported for this operation.

HTTP request

PATCH /admin/resourcePolicy/definitions/{resourcePolicyDefinition-uniqueName}

Request headers

Name Description
Authorization Bearer {token}. Required. Learn more about authentication and authorization.
Content-Type application/json or text/plain. Required.
Prefer create-if-missing. Required to create an object when the unique name doesn't exist.

Request body

For Content-Type: application/json, supply the following properties in the request body.

Property Type Description
rule resourcePolicyRule The Rego policy in rule.policy. The service computes rule.appliesTo and the definition metadata.

JSON-escape the Rego policy text and supply it as a string in rule.policy.

For Content-Type: text/plain, supply the unescaped Rego policy as the entire request body.

For either format, the unique name in the request URL must match the unique name in the Rego package. If the JSON body also specifies uniqueName, it must match as well. The package name supplies the version, such as package graph.require_application_name["1.0.0"]; applicability comes from __metadata__.selector. For details, see resourcePolicyDefinition.

Response

If the object is created, this method returns 201 Created and a resourcePolicyDefinition object. If the object is updated, it returns 204 No Content with no response body.

Examples

Example 1: Create an object that doesn't exist

Request

PATCH https://graph.microsoft.com/beta/admin/resourcePolicy/definitions/require_application_name
Content-Type: application/json
Prefer: create-if-missing

{
  "rule": {
    "policy": "package graph.require_application_name[\"1.0.0\"]\n\n__metadata__ := {\n  \"title\": \"Require an application name\",\n  \"description\": \"Require applications to have a display name\",\n  \"category\": \"identityAndTenant\",\n  \"selector\": {\n    \"resourceName\": \"microsoft.graph.application\",\n    \"operations\": [\n      \"create\",\n      \"updateOrUpsert\"\n    ]\n  }\n}\n\ndefault deny := {\"result\": false, \"reasons\": []}\n\ndeny := {\"result\": true, \"reasons\": [\"A display name is required.\"]} if {\n  object.get(input.resource, \"displayName\", \"\") == \"\"\n}\n"
  }
}

Response

HTTP/1.1 201 Created
Content-Type: application/json

{
  "@odata.type": "#microsoft.graph.resourcePolicyDefinition",
  "@odata.etag": "\"972dd856-91ff-4539-be09-50ea8235b76c\"",
  "uniqueName": "require_application_name",
  "createdDateTime": "2026-10-22T12:00:00Z",
  "lastModifiedDateTime": "2026-10-22T12:00:00Z",
  "displayName": "Require an application name",
  "description": "Require applications to have a display name",
  "category": "identityAndTenant",
  "version": "1.0.0",
  "deletedDateTime": null,
  "rule": {
    "appliesTo": {
      "operations": "create,updateOrUpsert",
      "resourceName": "microsoft.graph.application",
      "includeDerivedTypes": true
    },
    "policy": "package graph.require_application_name[\"1.0.0\"]\n\n__metadata__ := {\n  \"title\": \"Require an application name\",\n  \"description\": \"Require applications to have a display name\",\n  \"category\": \"identityAndTenant\",\n  \"selector\": {\n    \"resourceName\": \"microsoft.graph.application\",\n    \"operations\": [\n      \"create\",\n      \"updateOrUpsert\"\n    ]\n  }\n}\n\ndefault deny := {\"result\": false, \"reasons\": []}\n\ndeny := {\"result\": true, \"reasons\": [\"A display name is required.\"]} if {\n  object.get(input.resource, \"displayName\", \"\") == \"\"\n}\n"
  },
  "paramsSchema": null
}

Example 2: Update an existing object

Request

PATCH https://graph.microsoft.com/beta/admin/resourcePolicy/definitions/require_application_name
Content-Type: application/json
Prefer: create-if-missing

{
  "rule": {
    "policy": "package graph.require_application_name[\"1.1.0\"]\n\n__metadata__ := {\n  \"title\": \"Require an application name\",\n  \"description\": \"Require applications to have a display name\",\n  \"category\": \"identityAndTenant\",\n  \"selector\": {\n    \"resourceName\": \"microsoft.graph.application\",\n    \"operations\": [\n      \"create\",\n      \"updateOrUpsert\"\n    ]\n  }\n}\n\ndefault deny := {\"result\": false, \"reasons\": []}\n\ndeny := {\"result\": true, \"reasons\": [\"A display name is required.\"]} if {\n  object.get(input.resource, \"displayName\", \"\") == \"\"\n}\n"
  }
}

Response

HTTP/1.1 204 No Content

Example 3: Create an object with a plain-text policy

The service derives version 1.0.0 and the definition metadata from the Rego policy.

Request

PATCH https://graph.microsoft.com/beta/admin/resourcePolicy/definitions/require_application_name
Content-Type: text/plain
Prefer: create-if-missing

package graph.require_application_name["1.0.0"]

__metadata__ := {
  "title": "Require an application name",
  "description": "Require applications to have a display name",
  "category": "identityAndTenant",
  "selector": {
    "resourceName": "microsoft.graph.application",
    "operations": ["create", "updateOrUpsert"]
  }
}

default deny := {"result": false, "reasons": []}

deny := {"result": true, "reasons": ["A display name is required."]} if {
  object.get(input.resource, "displayName", "") == ""
}

Response

The response is shortened for readability.

HTTP/1.1 201 Created
Content-Type: application/json

{
  "@odata.type": "#microsoft.graph.resourcePolicyDefinition",
  "@odata.etag": "\"972dd856-91ff-4539-be09-50ea8235b76c\"",
  "uniqueName": "require_application_name",
  "displayName": "Require an application name",
  "version": "1.0.0"
}