Using Power Pages Admin API’s
Power Pages Admin APIs allow to perform admin operations like provisioning a website, restart a website etc. which administrator currently do manually in Power Platform admin center. The advantage of having API’s is that the process can be automated instead of performing the steps manually by an administrator.
Below are the available API's:
- Create Website
- Get Websites
- Get Website by Id
- Restart Website
- Delete Website
This article will take you through the steps to consume the API’s and sample Azure DevOps pipeline is created for this article to consume power pages admin api’s.
Registering an Application in Azure Active Directory
First things first, an application needs to be registered in Azure AD with required permissions to PowerPages website. Below are the steps:
- Navigate to the Azure AD app registration page and create a new registration. Give the application a name, and ensure the Single tenant option is selected. You can skip the redirect URI setup.
- Within your new app registration, navigate to the Manage - API Permissions tab. Under the Configure permissions section, select Add a Permission. On the dialog window that opens, select the APIs my organization uses tab, and then search for Power Platform API. You might see several entries with a name similar to this, so ensure you use the one with the GUID 8578e004-a5c6-46e7-913e-12f58912df43.
- Add the below permissions to the newly created application and grant admin consent for the tenant which gives the application required permission to read/write the Power Pages websites information.
Getting authentication token
For calling Portal Admin API’s, oAuth token needs to be generated. Below snippet can be used to generate the oAuth token. Currently only password grant type is supported and client credentials support will be added in future.
Content-Type: application/x-www-form-urlencoded
Host: login.microsoftonline.com
POST https://login.microsoftonline.com/YOUR_TENANT ID/oauth2/v2.0/token
BODY:
client_id={CLIENT_ID_FROM_AZURE_CLIENT_APP}&scope=https://api.powerplatform.com/.default&username={USER_EMAIL_ADDRESS}&password={PASSWORD}&grant_type=password
|
Create Website
Creates the website with given details. The request gives a 200 response or an error with the error message.
Authorization: Bearer eyJ0eXAiOi...
Host: api.powerplatform.com
POST https://api.powerplatform.com/powerpages/environments/<<ENVIRONMENT_ID>>/websites/?api-version=<<API_VERSION>>
BODY:
{
"dataverseOrganizationId" : "7cb52f94-70aa-46fa-a4ec-341a04fc25a4",
"name" : "<<WEBSITE NAME>>",
"subdomain" : "<<DOMAIN NAME>>",
"templateName" : "PowerPortals_BookMeeting",
"selectedBaseLanguage" : "1033"
}
|
List Websites
List websites will list all the power pages portals provisioned in an environment. The response will be JSON response with all the websites with the properties. The environment id can be retrieved from PowerApps admin portal.
Authorization: Bearer eyJ0eXAiOi...
Host: api.powerplatform.com
GET https://api.powerplatform.com/powerpages/environments/<<ENVIRONMENT_ID>>/websites/?api-version=<<API_VERSION>>
|
Get Website By Id
Get website gives the details of the Power pages site. The environment id and the website id can be retrieved from the PowerApps admin portal. The website id used in the request is from PowerApps admin portal is not the same as Dataverse website guid.
Authorization: Bearer eyJ0eXAiOi...
Host: api.powerplatform.com
GET https://api.powerplatform.com/powerpages/environments/<<ENVIRONMENT_ID>>/websites/<<WEBSITE_ID>>/?api-version=<<API_VERSION>>
|
Restart Website
Restarts the website for a given website id.
Authorization: Bearer eyJ0eXAiOi...
Host: api.powerplatform.com
POST https://api.powerplatform.com/powerpages/environments/<<ENVIRONMENT_ID>>/websites/<<WEBSITE_ID>>/restart/?api-version=<<API_VERSION>>
|
Delete Website
Deletes the Power pages website. Please note that the API call just delete a portal means its URL becomes inaccessible. Deleting a portal doesn't affect any portal configurations or solutions present in your environment, and they'll remain as-is. If you want to remove the portal configurations from your environment completely, delete the portal configurations manually. To delete, use the Portal Management app, and delete the corresponding website record for the portal.
Authorization: Bearer eyJ0eXAiOi...
Host: api.powerplatform.com
DELETE https://api.powerplatform.com/powerpages/environments/<<ENVIRONMENT_ID>>/websites/<<WEBSITE_ID>>/?api-version=<<API_VERSION>>
|
Sample Azure DevOps Pipeline:
For this article I have created an Azure DevOps YAML pipeline and used Azure PowerShell task to make a HTTP call. The below pipeline generates authentication token, creates the website and gets the available websites in an environment.
Sample script used to generate the authentication token(Get Auth Token step from above screenshot). The access token is stored in the pipeline variable highlighted so that the it can be used in next steps to call the Power Pages Admin API’s.
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/x-www-form-urlencoded")
$body = "client_id=$(clientId)&scope=https%3A%2F%2Fapi.powerplatform.com%2F.default&username=$(username)&password=$(password)&grant_type=$(grant_type)"
$response = Invoke-RestMethod 'https://login.microsoftonline.com/$(tenantid)/oauth2/v2.0/token' -Method 'POST' -Headers $headers -Body $body
$response = $response | ConvertTo-Json
Write-Host $response
$testJson1 = $response | ConvertFrom-Json
Write-Host $testJson1.access_token
$temp = $testJson1.access_token
Write-Host "##vso[task.setvariable variable=accessToken;]$temp"
|
Below script gets all the websites(Get All Portals step from screenshot) provisioned in an environment and stores the website id of the first website in a variable. As the response is a JSON object all JSON operations can be performed to get required information.
Write-Host "Getting All Portals"
$tempAccessToken = "$(accessToken)"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Bearer " +$tempAccessToken)
$response = Invoke-RestMethod 'https://api.powerplatform.com/powerpages/environments/$(environmentId)/websites/?api-version=2022-03-01-preview' -Method 'GET' -Headers $headers
Write-Host $response
$response1 = $response | ConvertTo-Json
$testJson1 = $response1 | ConvertFrom-Json
Write-Host $testJson1.value
$portalId = $testJson1.value[0].id
Write-Host "##vso[task.setvariable variable=portalId;]$portalId"
|
Demo:
Pipeline in action !!!
Hope you will explore the API’s and automate the Power Pages website admin actions!!!
Reference Articles: