cancel
Showing results for 
Search instead for 
Did you mean: 
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

4 REPLIES 4
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
Microsoft
Microsoft

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
Microsoft
Microsoft

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

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

November Power BI Update 768x460.png

Check it Out!

Click here to read more about the November 2021 Updates!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.