Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

PowerBI Rest API synchronous call in C#

Hi Everyone,

 

I want to refresh mutliple datasets synchornously using Rest API in C#. 2nd dataset refresh should be called only after 1st dataset refresh gets completed, Challenge which i face right now is, even before 1st gets completed, 2nd gets called and its getting SQL time out issue because of overload. 

 

1st dataset refresh takes minimum 30-45 mins and time might vary due to server load as well.  Please suggest how to handle this!!

 

foreach (var res in comparedResult)
{
HttpWebRequest request = System.Net.HttpWebRequest.CreateHttp(String.Format("https://api.powerbi.com/v1.0/myorg/groups/{0}/datasets/{1}/refreshes", res.GroupId, res.DatasetId));
//POST web request to create a datasource.
request.KeepAlive = true;
request.Method = "POST";
request.ContentLength = 0;
//Add token to the request header
request.Headers.Add("Authorization", String.Format("Bearer {0}", token));
Console.WriteLine("Dataset refresh request started for Dataset Id {0}", res.DatasetId);
//Write JSON byte[] into a Stream
using (Stream writer = request.GetRequestStream())
{
var response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Dataset refresh request done for Dataset Id {0}", res.DatasetId);
}
}

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi Ted,

 

Yeah you are almost right but we can use status column instead of start time / end time to identify whether API call got over or not. This is how I achieved it.

IRestResponse response = client.Execute(request);
var content = response.Content;

string statusName = string.Empty;
if (switchControl.ToLower() == "sync")
{
while (statusName.ToLower() != "completed")
{
statusName = GetRefreshHistoryByDatasetId(res.GroupId, res.DatasetId, token);
if (statusName.ToLower() != "completed")
{
Thread.Sleep(sleepTime * 60 * 1000); //In minutes
}
if (statusName.ToLower() == "failed")
{
break;
}
continue;
}
}

View solution in original post

7 REPLIES 7
rjoshi13
New Member

Can you please advise how you generated the token

Anonymous
Not applicable

Can you please list out the steps that you have performed for this activity  

Like Application  Registartion , Permissions granted ..Admin Consent required or not ..etc

Anonymous
Not applicable

Any reply please?

Anonymous
Not applicable

I got the solution, Let me know if anyone facing the same issue, I can help.

How did you solve this? I assumed you probably are using the REST API to retreive the refresh history which shows the state time and end time of each refresh history item. At first, a refresh history item will have a start time but no end time indiacting it is still in progress. Then you must poll the in-progress refresh history item to see when an end time is posted and you can begin the second refresh.

 

Is this how you accomplished your goal?

Anonymous
Not applicable

Hi Ted,

 

Yeah you are almost right but we can use status column instead of start time / end time to identify whether API call got over or not. This is how I achieved it.

IRestResponse response = client.Execute(request);
var content = response.Content;

string statusName = string.Empty;
if (switchControl.ToLower() == "sync")
{
while (statusName.ToLower() != "completed")
{
statusName = GetRefreshHistoryByDatasetId(res.GroupId, res.DatasetId, token);
if (statusName.ToLower() != "completed")
{
Thread.Sleep(sleepTime * 60 * 1000); //In minutes
}
if (statusName.ToLower() == "failed")
{
break;
}
continue;
}
}

Anonymous
Not applicable

How to run c# or curl command in PowerBI?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.