skip to Main Content

I am trying to add and change extended properties of devices registered in AzureAD using C#.
But I am getting the following error, I don’t know why…

Below is the part about the Graph API call. I used the document provided by MS Docs as it is, and it works normally in Graph API Explorer.

[C# Code]

var device = await graphclient.Devices
    .Request()
    .Filter(ftr)
    .Select("Id")
    .GetAsync();

string objectId = device.CurrentPage.First().Id.ToString().Trim();

var extattr = new Device
{
    AdditionalData = new Dictionary<string, object>(){
        {"extensionAttributes", "{"extensionAttribute1":"BYOD-Device2"}"}
    }
};

await graphclient.Devices[objectId]
     .Request()
     .UpdateAsync(extattr);

[ErrorCode]

Status Code: BadRequest
Microsoft.Graph.ServiceException: Code: Request_BadRequest
Message: A 'PrimitiveValue' node with non-null value was found when trying to read the value of the property 'extensionAttributes'; however, a 'StartArray' node, a 'StartObject' node, or a 'PrimitiveValue' node with null value was expected.

The current situation is that extensionAttribute1 has the value ‘BYOD-Device’.

Is there anyone out there who can help me solve this error?

2

Answers


  1. I tried to reproduce the same in my environment

    When using spectfic $filter and $search query parameters ,make sure to use the ConsistencyLevel header set to eventual and $count to true

    Headers:

    ConsistencyLevel=eventual 
    $count=true
    

    C# code:

    var queryOptions = new List<QueryOption>()
    {
        new QueryOption("$count", "true"),
        new QueryOption("$search", ""displayName:Android"")
    };
    var device = await graphclient.Devices
        .Request()
        .Filter(ftr)
       .Header("ConsistencyLevel","eventual")
        .Select("Id")
        .GetAsync();
    
    string objectId = device.CurrentPage.First().Id.ToString().Trim();
    
    var extattr = new Device
    {
        AdditionalData = new Dictionary<string, object>(){
            {"extensionAttributes", "{"extensionAttribute1":"BYOD-Device2"}"}
        }
    };
    
    await graphclient.Devices[objectId]
         .Request()
         .UpdateAsync(extattr);
    

    In azure ad extensionAttributes can be set while creation or updating device object.

    And Supports filter when value is null

    $filter (eq, not, startsWith, and eq on null values).

    Get the device details:

    I checked in one of my subscriptions with deviceId,
    could get its details.

    enter image description here

    Normally new extension attribute can be created for which doesn’t have any previous value or value is null .

    But here extensionattribute1 has already had a value in your case.

    "extensionAttributes": {
            "extensionAttribute1": "BYOD-Device",
            "extensionAttribute2": null,
            "extensionAttribute3": null,
            "extensionAttribute4": null,
            "extensionAttribute5": null,
            "extensionAttribute6": null,
            "extensionAttribute7": null,
           ...
    }
    

    But as it already has some value , to update it either new extension with another number has to be created or that attribute has to be explicitly made null and then updated.

     var queryOptions = new List<QueryOption>()
        {
            new QueryOption("$count", "true"),
            new QueryOption("$search", ""displayName:Android"")
        };
        var device = await graphclient.Devices
            .Request()
            .Filter(ftr)
           .Header("ConsistencyLevel","eventual")
            .Select("Id")
            .GetAsync();
        
        ......
    DeviceId = "xxxxxxx",
         ....
        AdditionalData = new Dictionary<string, object>()
        {
            {"extensionAttributes", "{"extensionAttribute1":"BYOD-Device"}"}
        }
    };
    
    await graphClient.Devices
        .Request()
        .AddAsync(device)
    

    Also note that some extension properties cannot be updated when using
    Linux OS

    Reference: https://learn.microsoft.com/en-us/graph/api/resources/device?view=graph-rest-1.0

    Login or Signup to reply.
  2. The value of the extensionAttribute1 property is not a JSON string, but a JSON object. Try

    AdditionalData = new Dictionary<string, object>(){
        {"extensionAttributes", new Dictionary<string, object>(){
            {"extensionAttribute1", "BYOD-Device2"}
        }}
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search