Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
deryl1974
Frequent Visitor

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 5
v-shex-msft
Community Support
Community Support

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

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
AmosHersch
Employee
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

AmosHersch
Employee
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

Hi AmosHersch,

 

I tried the below and got the same error. 

 

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

 

Thanks

Lloyd

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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.