Forum Discussion
Want to add Export Power BI Report Feature in my application where in embed the report
Hi Ibendlin,
By using the referred document, i tried to export a power bi report from my application using this code.
var powerBIReportExportConfiguration = new PowerBIReportExportConfiguration
{
Settings = new ExportReportSettings
{
Locale = "en-us",
},
Pages = pageNames?.Select(pn => new ExportReportPage(pageName = pn)).ToList(),
};
var exportRequest = new ExportReportRequest
{
Format = format,
PowerBIReportConfiguration = powerBIReportExportConfiguration,
};
var url = XXX;
var content = new Dictionary<string, string>();
content["grant_type"] = "XXX";
content["resource"] = "XXX";
content["username"] = XXX;
content["password"] = XXX;
content["client_id"] = XXX;
var response = await APIHelper.MakeAsyncRequest(url, content);
var tokenresult = response.Content.ReadAsStringAsync().Result;
var OAuthResultModel = JsonConvert.DeserializeObject<OAuthResultModel>(tokenresult);
var authenticationResult = OAuthResultModel;
var credentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
var client = new PowerBIClient(credentials);
var reportsResult = await client.Reports.ExportToFileInGroupAsync(GroupID, ReportID, exportRequest);
While executing the last line, code break and through an exception, i.e.:
{"Operation returned an invalid status code 'Forbidden"}
Test it out in the sandbox.
https://docs.microsoft.com/en-us/rest/api/power-bi/reports/exporttofileingroup
- saadashraf6 years ago
Helper I
After testing it out in the sandbox, I fix few thing in my code and now it is executing completely.
Here is the code , which i did.
public async Task<ExportedFile> ExportPowerBIReport(Guid ReportID, Guid GroupID, FileFormat format, int pollingtimeOutInMinutes, CancellationToken token, IList<string> pageNames = null)
{
try
{
var exportId = await PostExportRequest(ReportID, GroupID, format, pageNames);
var export = await PollExportRequest(ReportID, GroupID, exportId);
if (export == null || export.Status != ExportState.Succeeded)
{
// Error, failure in exporting the report
return null;
}
return await GetExportedFile(ReportID, GroupID, export);
}
catch(Exception ex)
{
// Error handling
throw ex;
}
}but the problem i am facing right now is after executing the last line, i.e.
return await GetExportedFile(ReportID, GroupID, export);it takes me to the browser with a error pop-up. just "error" written on it.. and the report didn't downloaded.
My report is consist of 3 pages, so do i am referring the right document ? or i should go with this one ?https://docs.microsoft.com/en-us/power-bi/developer/embedded/export-paginated-report