Skip to main content

Avoiding workspace loops by expanding navigation properties in the GetGroupsAsAdmin API

Headshot of article author Harjinder Raheja

We are excited to announce theย recentย release of support for $expand in theย GetGroupsAsAdminย API!ย As aย Power BI service adminย if you needย to list all workspaces inย yourย tenant,ย including their users, reports, dashboards, and datasets,ย $expandย helpsย youย do this quickly and efficiently.

Support for $expand in theย GetGroupsAsAdminย API enables you to retrieve the details of the navigation properties for users, reports, dashboards, and datasets directly in the workspace properties.ย You no longerย need to loop throughย each workspace and call 4 separate APIs. With $expand, you can accomplishย that work with aย singleย API call.ย This makes your solutionsย dramatically simpler, more intuitive,ย faster to develop,ย and easier to maintain.

Using the $expand query option is very straight forward.ย Hereโ€™s the API call to get the first 5000 workspaces in your tenant.

https://api.powerbi.com/v1.0/myorg/admin/Groups?$top=5000

This just returns a list of workspaces names, GUIDs, and other metadata. Nowย letโ€™s see how easy it is toย get all the users, reports, dashboards, and datasets in these workspaces.

To do this just appendย $expand and specify which objects youโ€™d like to expand as shown below.

https://api.powerbi.com/v1.0/myorg/admin/Groups?$top=5000&$expand=users,reports,dashboards,datasets

The following listing shows a sample result returned for a single workspace:

  "value": [ 
    { 
      "id": "183dcf10-47b8-48c4-84aa-f0bf9d5f8fcf", 
      "isReadOnly": false, 
      "isOnDedicatedCapacity": false, 
      "name": "Sample Group 2", 
      "description": "Deleted sample group", 
      "type": "Workspace", 
      "state": "Deleted", 
      "users": [ 
        { 
          "emailAddress": "john@contoso.com", 
          "groupUserAccessRight": "Admin" 
        } 
      ], 
      "reports": [ 
        { 
          "datasetId": "cfafbeb1-8037-4d0c-896e-a46fb27ff229", 
          "id": "5b218778-e7a5-4d73-8187-f10824047715", 
          "name": "SalesMarketing" 
        } 
      ], 
      "dashboards": [ 
        { 
          "id": "69ffaa6c-b36d-4d01-96f5-1ed67c64d4af", 
          "displayName": "SalesMarketing", 
          "isReadOnly": false 
        } 
      ], 
      "datasets": [ 
        { 
          "id": "cfafbeb1-8037-4d0c-896e-a46fb27ff229", 
          "name": "SalesMarketing", 
          "addRowsAPIEnabled": false, 
          "configuredBy": "john@contoso.com", 
          "isRefreshable": true, 
          "isEffectiveIdentityRequired": false, 
          "isEffectiveIdentityRolesRequired": false, 
          "isOnPremGatewayRequired": false 
        } 
      ] 
    } 
  ]

If you have more the 5,000 workspaces, then use the $skip parameter to page through the remaining workspaces with a batch size of 5,000.

https://api.powerbi.com/v1.0/myorg/admin/Groups?$top=5000&$skip=5000&$expand=users,reports,dashboards,datasets

Ifย your Power BI tenant has 50,000 workspaces, you can retrieve allย thatย information with just 10 calls. Compare that to a loop through 50,000 individual workspaces with 3 separate calls to get the reports, dashboards, and datasetsย – a dramatically easier solution.

The workspaceย PowerShellย cmdlets donโ€™t support $expand yet. However,ย you can use the Invoke-PowerBIRestMethodย cmdlet to make the calls. Just keep in mind that you must escape special characters in the URL. For example, %24 represents the dollar sign in $expand.ย Below is an example you can try.

Login-PowerBI 
  
$result = Invoke-PowerBIRestMethod -Url "https://api.powerbi.com/v1.0/myorg/admin/Groups?%24top=1&%24expand=reports,dashboards,datasets" -Method Get 
  
$workspaceContents = $result | ConvertFrom-Json 
  
$firstWorkspace = $workspaceContents.value[0] 
  
Write-Host "" 
Write-Host "Reports in first workspace:" 
Write-Host "---------------------------" 
$firstWorkspace.reports | Format-List 
Write-Host "---------------------------" 
  
Write-Host "" 
Write-Host "Dashboards in first workspace:" 
Write-Host "---------------------------" 
$firstWorkspace.dashboards | Format-List 
Write-Host "---------------------------" 
  
Write-Host "" 
Write-Host "Datasets in first workspace:" 
Write-Host "---------------------------" 
$firstWorkspace.datasets | Format-List 
Write-Host "---------------------------" 

Here is the output:

To get started quickly, you canย try the $expand query option in theย Webย browser. Just go to theย Admin – Groups GetGroupsAsAdminย page in the Power BI REST API Reference. There you canย click on Try It, sign in, and specify the $top and $expand parameters as discussed earlier. Optionally, you can also define the $skip parameter. Just make sure you click on the Plus (+) sign to apply the additional parameters to the URL.ย As a bonus, youโ€™ll noticeย the URL displayed under Request Preview includes all parameters nicely escaped, which makes it easy to copy and paste the URL into a Invoke-PowerBIRestMethodย call. The following screenshot shows you the Try It feature in action.

And thatโ€™s it for a brief introduction of the recently added support for the $expand query option in theย GetGroupsAsAdminย API!

To get more informationย andย keep up to date for API improvements,ย occasionally check theย .NET SDK for Power BIย and theย Open Source repository for Power BI cmdletsย on GitHub.

Learn more about Power BI admin API