Forum Discussion
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);
}
}
- Anonymous7 years ago
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;
}
}
7 Replies
- AnonymousNot applicable
Any reply please?
- AnonymousNot applicable
I got the solution, Let me know if anyone facing the same issue, I can help.
- TedPattisonMicrosoft Employee
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?
- rjoshi13New Member
Can you please advise how you generated the token
- AnonymousNot 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