Refresh Power BI Dataset programmatically from Dynamics 365 CRM

By | October 18, 2019


Many of you are aware of manual refresh and schedule refresh feature of Power BI. Schedule refresh have refresh limit set based on the type of Power BI license. Users can add power BI report to CRM via CRM dashboard and Power BI dashboard. Also user’s data gets updated frequently and refreshing Power BI dataset each time from Power BI service would be time consuming as every time user needs to go to the Power BI Service for refreshing Power BI report.

Apart from that, user also needs to manually configure schedule refresh for the dataset and specify the refresh timings and time zone in the Power BI Service. So having a button on CRM Dashboard page to automatically refresh the dataset in Power BI Service is more convenient for the user as there will be no further need to switch to the Power BI service for refreshing dataset.

In this blog we will see how to refresh Power BI report from Dynamics 365 CRM.
For this, we will create a global action in which we will call a custom assembly to make refresh request of Power BI. Now for making call to refresh request of Power BI we have the following two steps:

1. Getting access token from Power BI
In order to get access token from Power BI Service we need to register a native application in Azure.
Please check the below link for creating native application in Azure AAD.

After registering an application, assign required Power BI Service delegated permission to native application.
Copy the client Id from the native application as shown in the below screenshot:

Refresh Power BI Dataset programmatically

This Client Id will be used for getting access token from Power BI Service.

Use the below code to get access token from Power BI Service.

PowerBIAccessToken token = null;
string oauthurl = string.Format("");

string reqBody = string.Format("client_id={0}&grant_type={1}&resource={2}&username={3}&password={4}",

Uri.EscapeDataString(clientId), Uri.EscapeDataString(grantType),

Uri.EscapeDataString(resource), Uri.EscapeDataString(userName), Uri.EscapeDataString(password));

tracingService.Trace("Getting Access Token");

HttpClient client = new HttpClient();

HttpContent content = new StringContent(reqBody);

content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");

using (HttpResponseMessage response = await client.PostAsync(oauthurl, content))


if (response.IsSuccessStatusCode)


DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(PowerBIAccessToken));

Stream json = await response.Content.ReadAsStreamAsync();

//Gets Access Token

token = (PowerBIAccessToken)serializer.ReadObject(json);



By using the above code we will get access token which will be used while making refresh request in Power BI Service.

2. Refresh Power BI Dataset

For making Power BI refresh request, we need dataset Id which need to be refreshed. We can get dataset id from Power BI service.

Now we will use this dataset id to refresh Power BI dataset. Below is the sample code for the same.

 HttpClient refreshClient = new HttpClient();
                    refreshClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", powerBIToken.access_token);
                    HttpContent content = new StringContent("{'notifyOption':''}", Encoding.UTF8, "application/json");
                    HttpResponseMessage result = refreshClient.PostAsync("", content).Result;
                        if (result.IsSuccessStatusCode)
                            tracingService.Trace("DataSet Refresh Request Submitted Successfully.");


Now we are using datasets under ‘My Workspace’ but you can also refresh Power BI dataset from other workspace using this request.

First, we need to call this assembly in the global action.

Next we need a JavaScript code to call the Action (global). Given below is the sample code for the same:

let request: any = function ()


this.getMetadata = function () {

return {

boundParameter: null,

parameterTypes: {


operationType: 0, // This is an action. Used '0'.

operationName: "new_RefreshPowerBIDataset"





var tempRequest = new request();

//Execute global action.;

Now we will add new ribbon button ‘Refresh Power BI’ on CRM home page as shown below:

Refresh Power BI Dataset programmatically

We will call the JavaScript written on button click event to refresh Power BI dataset. So now when you click on the button, one confirmation Dialog will appear asking confirmation to refresh Power BI Dataset. Given below is the screenshot of the same:

Refresh Power BI Dataset programmatically

After you click on ‘Ok’, Power BI Dataset will start refreshing and you can see the same in the Power BI Service. Once refresh is completed, Refresh Data will also get changed in the Power BI Service.

Refresh Power BI Dataset programmatically

Refresh Power BI Dataset programmatically

As seen in the above Screenshot, last refresh date for dataset gets updated in the Power BI Service.

Power BI Refresh request will only trigger if you have set Date Source Credentials for the Power BI Service.

Note: For Power BI Free license, Daily refresh limit is set to 8 refreshes per day. So once it reaches the maximum refresh limit, it will not refresh the dataset further.


In this way, you can easily refresh Power BI Dataset programmatically from Dynamics 365 CRM.

Leave a Reply

Your email address will not be published. Required fields are marked *