Forum Discussion

deryl1974's avatar
deryl1974
Frequent Visitor
4 years ago

Export PDF from paginated report in c#

Hi All,

 

I am trying to export a paginated report into pdf from c# application. Below is my code:

 

private async Task<string> PostExportRequest(Guid reportId, Guid groupId)
{
try
{
var paginatedReportExportConfiguration = new PaginatedReportExportConfiguration()
{
FormatSettings = new Dictionary<string, string>() { { "PageHeight", "14in" }, { "PageWidth", "8.5in" }, { "StartPage", "1" }, { "EndPage", "1" } }
};

var exportRequest = new ExportReportRequest { Format = FileFormat.PDF, PaginatedReportConfiguration = paginatedReportExportConfiguration };

var _apiUrl = new Uri("https://app.powerbi.com/groups/<groupId>/rdlreports/<reportId>/ExportTo");
var handler = new System.Net.Http.HttpClientHandler() { UseDefaultCredentials = false };
string clientID = "xxx-xxx-xxx-xxx-xxx-xxxx";
string secret = "xxx-xxx-xxx-xxx-xxx-xxxx";
var credentials = new Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential(clientID, secret);
var AuthorityUrl = "https://login.microsoftonline.com/consumers/oauth2/v2.0/<my application tenentid>";
var authenticationContext = new AuthenticationContext(AuthorityUrl);
var authenticationResult = await authenticationContext.AcquireTokenAsync("https://analysis.windows.net/powerbi/api", credentials).ConfigureAwait(false);
var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");
var pbClient = new PowerBIClient(_apiUrl, tokenCredentials);
var export = await pbClient.Reports.ExportToFileInGroupAsync(groupId, reportId, exportRequest);
return export.Id;
}
catch (Exception ex){} return null;

}

 

 

Whenever i try to run the code I get auth token correctly, however ExportToFileInGroupAsync gives me an error of below:

 

{"Operation returned an invalid status code 'InternalServerError'"}

 

Does anybody know what I am doing wrong here?

 

Appreciate your help in advance.

 

Thanks
Lloyd

5 Replies

  • AmosHersch's avatar
    AmosHersch
    Microsoft Employee

    Hi Lloyd.

     

    I believe that in the URL "rdlreports" should be "reports" (same API URL for both power bi reports and paginated reports)

    Can you please try with:

    var _apiUrl = new Uri("https://app.powerbi.com/groups/<groupId>/reports/<reportId>/ExportTo");

     

    Thanks

      • keegan's avatar
        keegan
        New Member

        HI Lloyd,

        Did you have any luck with this one.  I've started the same process and am getting the same error.  My code is identical to yours.

        Thanks

        Keegan

  • AmosHersch's avatar
    AmosHersch
    Microsoft Employee

    Hi Lloyd,

     

    Sorry for the confusion. Since you're using Power BI C# SDK for the API call you should just use the base URL instead of the full URL of the API:

    var _apiUrl = new Uri("https://api.powerbi.com/");

     

    The SDK will build the full URL and send the request to Power BI

  • Anonymous's avatar
    Anonymous
    Not applicable

    HI deryl1974,

    AFAIK, the 'internal server error' normally means you may send the wrong contents or send to the wrong target so that the remote server can't respond to these requests.

    You can take a look at the following link if helps:

    REST API error code 500 handling - Stack Overflow

    Regards,

    Xiaoxin Sheng