cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Helper I
Helper I

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

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
New Member

Can you please advise how you generated the token

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

Helper I
Helper I

Any reply please?

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?

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

Anonymous
Not applicable

How to run c# or curl command in PowerBI?

Helpful resources

Announcements
secondImage

Happy New Year from Power BI

This is a must watch for a message from Power BI!

December Update

Check it Out!

Click here to read more about the December 2020 Updates!

Community Blog

Check it Out!

Click here to read the latest blog and learn more about contributing to the Power BI blog!

Get Ready for Power BI Dev Camp

Get Ready for Power BI Dev Camp

Mark your calendars and join us for our next Power BI Dev Camp!.