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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

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
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.