Automating PowerApps Portals Cache Clearance using PowerShell Scripts


Necessity is the mother of invention. I like to extend the work "Innovation" to "workaround", "Solution" and anything that solves a problem.  During my work I come across many solutions made by others in the community and I am grateful to each individual with contributions to our community. When I cannot find a solution, I try to put  few solutions advised by other community members and come up with something newer. Then it is my turn to give it back to the community so the next person can extend it to the next level and that is the way we grow as a community.

The Challenge

When working with PowerApps portals, for every change in the server configuration (site settings, content snippets, liquid template, etc.) we will have to Clear Cache. There are three issues with clearing cache manually.

  1. Clearing Cache requires to login to the portal as Admin and also must be on the same browser. Sometimes you want to give access to power users without Admin rights and they cannot clear cache because they are not admins.
  2. Clearing cache is a manual process. So every time you change something, you will need to save your record, go to browser tab and click on Clear Cache manually. (This may look so trivial for you but believe me, when you are under pressure and making constant changes, this is a terrible experience).
  3. When you make changes in client script files such as CSS file, clearing cache does not work. You will need to try your CSS changes in an in-private mode to see the new client files loaded in your browser.

The solution

Using a Powershell script to open a new Microsoft Edge window when you are ready for testing. All you have to do is the following:

  1. Save your changes in CRM (like WebForms, Content Snippets, etc.)
  2. If you have any web files i.e. css files, use "Portal Code Editor" or any other text editor to upload your code.
  3. Run the Powershell script from using PowerShell.

This will save you from:

  1. Being logged in as Admin in your browser all the time
  2. Maximisimg browser window and pressing Clear Cache button every time you make changes
  3. Open In-private window to ensure your client files are not cached.

Implementing the solution

You will need to create a powershell script and run it as administrator. The powershell script expects you to provide a URL including auth token and send a request. The powershell script runs Microsoft Edge in-private mode and prints 'true' as output. If the token is expired you will get an exception and you need to follow the below steps.

You will need to fill few variables in your PowerShell script. Once you get correct values of these variable's you can put them in the script and run the script. The important ones are:

  • $tokenUrl
  • $url

$tokenUrl

Get the AuthToken from Portals by going to this link

https://login.microsoftonline.com/common/oauth2/authorize?response_type=token&client_id=a8f7a65c-f5ba-4859-b2d6-df772c264e9d&resource=https%3A%2F%2Fportal.dynamics.com&redirect_uri=https%3A%2F%2Fmake.powerapps.com%2Fauth&response_mode=fragment

Couple of notes here:

  • The Access Token is refreshed every hour. You will need to access the above link every time the token is expired (Basically you will receive an exception when the powershell script runs so you know the token is expired).
  • Once you go to the above link, you will see the response on the address bar. Like the below URL https://make.powerapps.com/auth#access_token=ey…… .
  • Copy the entire URL and replace auth# with auth? Like https://make.powerapps.com/auth?access_token=ey….
  • If you are asked to login, you will need to login using your power apps login credentials.

 $URL

To form the URL, you will need your portal's region and also portal ID. You can get your region and portal ID from _services/about page.


To form the URL, replace the [Region] in the following link with your region and Portal ID with your Portal ID: https://portalsitewide-[Region].portal-infra.dynamics.com/api/v1/powerportal/ClearCache?portalId=[PortalID]…

The Script

# The below URL is to get the Auth Token
# Use this URL "AS IS"
# https://login.microsoftonline.com/common/oauth2/authorize?response_type=token&client_id=a8f7a65c-f5ba-4859-b2d6-df772c264e9d&resource=https%3A%2F%2Fportal.dynamics.com&redirect_uri=https%3A%2F%2Fmake.powerapps.com%2Fauth&response_mode=fragment

#Once you obtai the auth token response, replace the /auth# in the URL with auth? to look like the below URL
$tokenUrl =[System.Uri] "https://make.powerapps.com/auth?access_token=eyJ0..."

Add-Type -AssemblyName System.Web

$ParsedQueryString = [System.Web.HttpUtility]::ParseQueryString($tokenUrl.Query)

$token = $ParsedQueryString[0]

# You need to run the script as Administrator
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine

# Replace OCE with your region code and Portal ID with your own Portal ID
$url = "https://portalsitewide-oce.portal-infra.dynamics.com/api/v1/powerportal/ClearCache?portalId=3b..."

$headers = @{Authorization = "Bearer $token"}

$response = Invoke-RestMethod -ContentType "$contentType" -Uri $url -Method "Post" -Headers $headers -UseBasicParsing

$response

# Replace YOURPORTALURL with your own portal URL
cmd.exe /c start shell:AppsFolder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge -private https://YOURPORTALURL.powerappsportals.com/

Comments

Popular Posts