How to read "departmentCode" in GraphAPI Users response

Abhijith RP 0 Reputation points
2024-09-03T23:44:49.76+00:00

Hello Microsoft Graph Team,

We are trying to extract departmentCode along with department information from Graph API. Could you please guide us on how to achieve this?

We have already checked the users metadata, but we could not find any information that matches the department code.

Business Need: We need an attribute that uniquely identifies a department, and remains same even if the department name is changed frequently.

Example of department:

 "department": "Information Technology"

We are looking for departmentCode: SomeCodeWhichUniquelyDefines.

Thank you,

Abhijith Panchakshari

Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Hitesh Pachipulusu - MSFT 3,620 Reputation points Microsoft External Staff
    2024-09-04T07:06:12.2533333+00:00

    Hello Abhijith RP,

    Thank you for reaching to Microsoft Support!To achieve this, you can use Microsoft Graph API’s extension attributes to store and retrieve custom properties like departmentCode. Here’s a step-by-step guide to help you:

    1. Register an Application:
      • Go to the Azure Portal.
      • Navigate to Azure Active Directory > App registrations > New registration.
      • Register your application.
    2. Grant API Permissions:
      • In your registered application, go to API permissions.
      • Add the following permissions:
      • Directory.ReadWrite.All
      • User.ReadWrite.All
      • Grant admin consent for these permissions.
    3. Create and Assign Extension Attributes:
      • Use the Microsoft Graph API to create an extension attribute. You can use the schemaExtensions endpoint to define a new schema extension. Please refer documentation.
      • Example request to create a schema extension:
    POST https://graph.microsoft.com/v1.0/schemaExtensions
    Content-Type: application/json
    {
      "id": "Department",
      "description": "Schema extension for department code",
      "targetTypes": ["User"],
      "properties": [
        {
          "name": "departmentCode",
          "type": "String"
        }
      ]
    }
    
    

    image (15)

    • Assign the extension attribute to a user:
    PATCH https://graph.microsoft.com/v1.0/users/{user-id}
    Content-Type: application/json
    {
      "yourSchemaExtensionName(id generated in step3)": "SomeCodeWhichUniquelyDefines"
    }
    

    image (16)

    Retrieve the Extension Attribute:

    • To retrieve the Department Attribute for a user, use the following request:
    GET https://graph.microsoft.com/v1.0/users/{user-id}?$select=yourSchemaExtensionName
    

    image (17)

    This approach allows you to store a unique departmentCode that remains consistent even if the department name changes.

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.


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.