Can't expand newly created Open Extension in MS Graph

Viewed 187

TLDR version: This was caused by not encoding the Graph query string before calling Graph - not an API issue!

I've created a new Open Extension for a particular user. I can read the user, or the Open Extension specifically, but I can't use $expand to return the user with the Open Extension together in a single call.

I'm using Application authentication to AAD (with a secret), and that may or may not have something to do with it. Every example I've found uses the /v1.0/me shortcut in Graph Explorer.

EDIT: I quickly got an answer that did not take this into account. The MS Graph Explorer only allows for authentication via a user account and does not help me

I can consistently add, read and delete an extension. The only issue is expanding the extensions attributes when reading.


$uri = "https://graph.microsoft.com/v1.0/users/user@domain.com.au/extensions"

$body = '{
    "@odata.type":"microsoft.graph.openTypeExtension",
    "extensionName":"au.com.domain.customAttributes",
    "shoesize":"large"
}'

$query = Invoke-RestMethod -Method Post -Uri $uri -ContentType "application/json" -Body $body -Headers @{Authorization = "Bearer $token"} -ErrorAction Stop

#Modify an existing custom attribute
$uri = "https://graph.microsoft.com/v1.0/users/user@domain.com.au/extensions/au.com.domain.customAttributes"

$body = '{
    "shoesize":"small"
}'

$query = Invoke-RestMethod -Method Patch -Uri $uri -ContentType "application/json" -Body $body -Headers @{Authorization = "Bearer $token"} -ErrorAction Stop

## Read user without extension attributes
$uri = "https://graph.microsoft.com/v1.0/users/user@domain.com.au"
$user = Invoke-RestMethod -Method Get -Uri $uri -Headers @{Authorization = "Bearer $token"} -ErrorAction Stop
Write-Host $user

## Read extension attributes only in separate call
$uri = "https://graph.microsoft.com/v1.0/users/user@domain.com.au/extensions/au.com.domain.customAttributes"
$customAttributes = Invoke-RestMethod -Method Get -Uri $uri -Headers @{Authorization = "Bearer $token"} -ErrorAction Stop
Write-Host $customAttributes

## Delete a custom extention
$uri = "https://graph.microsoft.com/v1.0/users/user@domain.com.au/extensions/au.com.domain.customAttributes"
$customAttributes = Invoke-RestMethod -Method DELETE -Uri $uri -Headers @{Authorization = "Bearer $token"} -ErrorAction Stop

Write-Host $customAttributes

I need to be able to use a filter to get 100 users from 55000, including all of their open extension attributes.

EDIT: I suspect this may be a query parameters related problem and I think I'll sleep on this before rewriting the question maybe?

This works for example:

$uri = "https://graph.microsoft.com/v1.0/users"
$users = Invoke-RestMethod -Method Get -Uri $uri -Headers @{Authorization = "Bearer $token"}  -ContentType "application/json" -ErrorAction Stop 

This gives me the first 100 users for my tenant. Adding any query parameter at all breaks the request. I get a 400 error, even if example the same Graph call in Graph Explorer gives a meaningful response.

I'm using Invoke-RestMethod in powershell to make the calls to Graph... I suspect I need to try a different way of making calls or try passing extra parameters to Invoke-RestMethod

1 Answers

but I can't use $expand to return the user with the Open Extension together in a single call

Extensions show up in Microsoft Graph within the Extensions collection. So you can use $expand to return the users with the Open Extension together in a single call.

https://graph.microsoft.com/v1.0/users?$expand=extensions

I created the same extension for a user. I can find it in the response.

enter image description here

Update:

I tried with client credential flow, it also works. Here are the detailed steps.

  1. Register an application and grant User.ReadWrite.All permission, remember to grant admin consent.

enter image description here

2.get access token

enter image description here

3.call the api with access token

enter image description here

Update: run it using powershell

enter image description here

Related