Forum Discussion

nimit's avatar
nimit
Frequent Visitor
4 years ago

Power BI Report Server - PBIX report export programmatically

Hello,

 

I have installed Power BI Report Server in VM and have serveral report file with .PBIX exension file. I want to export PBIX report in excel format using C# program. I have tried Microsoft API for power BI and it is working with Power BI app (app.powerbi.com).

 

How can we use Microsoft Power BI API 2.0 for Power BI Report Server.

 

 

Thanks,

Nimit

2 Replies

  • v-henryk-mstf's avatar
    v-henryk-mstf
    Community Support

    Hi nimit ,

     

    You need to import the relevant program interface first, and then provide some methods to export the relevant report as follows.

    links:IReports Interface (Microsoft.PowerBI.Api.V2) - Azure for .NET Developers | Microsoft Docs

     

    In addition you can also refer to the following blog to give relevant information, I hope it will help you.

    Accessing REST API In PowerBI Report Server (c-sharpcorner.com)


    If the problem is still not resolved, please provide detailed error information and let me know immediately. Looking forward to your reply.


    Best Regards,
    Henry


    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • nimit's avatar
      nimit
      Frequent Visitor

      Hi Henery,

       

      Thanks for your kind response.

      Here I my code which will help you for more support.

       

      1. Power bi report server URL - "https://powerbireport.server.com/" - using this url I can access power bi report in browser 

       

      2. Power BI Report - report ID (itemID field in catalog table of db) - xxxxxxx-1111-22222-B8A7-6DD7D668ADA8 

       

      client.Reports.ExportToFile(reportId, exportRequest); - line of code throws an error  -

      Microsoft.Rest.HttpOperationException: 'Operation returned an invalid status code 'NotFound''

       

      Please confirm above two items are used correctly and how to resolve this error.

       


      var credentials = new TokenCredentials(TokenManager.GetAppOnlyAccessToken(), "Bearer");
      using (var client = new PowerBIClient(new Uri("https://powerbireport.server.com/"), credentials))
      {
      var exportRequest = new ExportReportRequest
      {
      Format = FileFormat.PDF,
      PowerBIReportConfiguration = new PowerBIReportExportConfiguration
      {
      Settings = new ExportReportSettings
      {
      IncludeHiddenPages = false
      }
      }
      };

      var reportId = new Guid("xxxxxxx-1111-22222-B8A7-6DD7D668ADA8");
      var export = client.Reports.ExportToFile(reportId, exportRequest);
      string exportId = export.Id;
      do
      {
      System.Threading.Thread.Sleep(10000);
      export = client .Reports.GetExportToFileStatus(reportId, exportId);
      Console.WriteLine(" - Export status: " + export.PercentComplete.ToString() + "% complete");
      } while (export.Status != ExportState.Succeeded && export.Status != ExportState.Failed);

      if (export.Status == ExportState.Failed)
      {
      Console.WriteLine("Export failed!");
      }

      if (export.Status == ExportState.Succeeded)
      {
      string FileName = "test.pdf";

      string FilePath = "C:\\PowerBiReport\\" + FileName;

      Console.WriteLine(" - Saving exported file to " + FilePath);
      var exportStream = client .Reports.GetFileOfExportToFile(reportId, exportId);
      FileStream fileStream = File.Create(FilePath);
      exportStream.CopyTo(fileStream);
      fileStream.Close();
      }

      Console.WriteLine();

      }