azuremicrosoft-graph-apiazure-rest-apiazure-securityazure-information-protection

How to retrieve sensitivity labels associated with user using API or .NET?


I’m trying to pull a list of sensitivity labels associated with a specific user using the Microsoft Graph API, but I’m hitting a wall. Here’s the API call I tried:

GET https://graph.microsoft.com/v1.0/users/{id}/security/informationProtection/sensitivityLabels

Unfortunately, I got a 400 Bad Request error and the message said something about an unexpected segment:

{
    "error": {
        "code": "Request_BadRequest",
        "message": "Unexpected segment DynamicPathSegment. Expected property/$value.",
        ...
    }
}

I also tried this in C# like this:

var result = await graphClient.Users["{id}"].Security.InformationProtection.SensitivityLabels.GetAsync();

But I ran into this error:

CS1061 'UserItemRequestBuilder' does not contain a definition for 'Security'...

Has anyone successfully retrieved sensitivity labels for a user? If so, could you point me in the right direction? I’d really appreciate any help!


Solution

  • Note that, retrieving sensitivity labels associated with user via MS Graph API is available only in Beta version not in v1.0.

    I have below list of sensitivity labels associated with user of my tenant:

    enter image description here

    Now, I registered one app and granted InformationProtectionPolicy.Read.All permission of Application type with admin consent like this:

    enter image description here

    When I ran below API with v1.0 endpoint using token generated with client credentials flow, I too got same error:

    GET https://graph.microsoft.com/v1.0/users/{id}/security/informationProtection/sensitivityLabels
    

    Response:

    enter image description here

    To resolve the error, make sure to use /beta endpoint where I got the response with labels details successfully as below:

    GET https://graph.microsoft.com/beta/users/{id}/security/informationProtection/sensitivityLabels
    

    Response:

    enter image description here

    Reference:

    List sensitivityLabels - Microsoft Graph beta