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
keegan
New Member

Unable to export Paginated report to PDF

Hi guys,

I've followed a number of posts online, howver cannot export a paginated report to pdf.

my code so far seems to be on the right track, although I have no idea..  I"ve placed some comments in bold in my code below.

 

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

var _apiUrl = new Uri("https://api.powerbi.com/"); //note: I've tried a heap of different URLs here!!
// _apiUrl = new Uri("https://app.powerbi.com/groups22/<groupId>/reports/<reportId>/ExportTo");
var handler = new HttpClientHandler() { UseDefaultCredentials = false };
string clientID = "my-client-id";
string secret = "my-secret";
var credentials = new ClientCredential(clientID, secret);
var AuthorityUrl = "https://login.microsoftonline.com/consumers/oauth2/v2.0/azurecode";
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");

//At this point I have authenticated my app in azure and have a valid token
var pbClient = new PowerBIClient(_apiUrl, tokenCredentials);

//It hangs on the next call and returns either (not found or internal server error, depending on the URL i try)
var export = await pbClient.Reports.ExportToFileInGroupAsync(groupId, reportId, exportRequest);
}
catch (Exception ex)
{

}

Does anyone have any ideas or can point me in the right direction?  I have a premium per user account.

In addition to the above, the report I am trying to print expects a parameter which I pass via the following:

var paginatedReportExportConfiguration = new PaginatedReportExportConfiguration()
{
ParameterValues = new List<ParameterValue>()
{
{ new ParameterValue() {Name = "Student ID", Value = "22364e64-438f-4a66-8d51-53ccea48ffd3"} },
},
};

Also; The report I'm trying to print was imported from SQL reporting services (not sure this matters....).

 

Any help would be great, its been a long week!

1 ACCEPTED SOLUTION

Hi @keegan ,

 

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

 

I think that either the report ID you're using doesn't exist, or the user/service principal which is used when calling the API doesn't have access to the workspace of the report.

You should add the user/service principal as a member to the workspace.

 

 

View solution in original post

4 REPLIES 4
keegan
New Member

Hi @AmosHersch,

Thank you so much!!!

I did not have the service prinicple account from azure registered under the access account of my workspace (woohooo!!!).

I've added it now and it has pushed through without error.  So this and correcting the authority url allowed me to poll the export of my report!

I have no idea what to do next, but I'll get to it.

Thanks again so much for your help, I really appreciate it!

Keegan

AmosHersch
Employee
Employee

Hi @keegan ,

Did you see the solution here: https://community.powerbi.com/t5/Developer/REST-API-Get-Access-Token/m-p/1895937#:~:text=The%20resou....

 

I'm not sure what is the cause for the error you get, but I think your authority URL is different. the API URL you're using seems correct.

If you still can't get it to work please see in the error response you get has request ID in the header, I might be able to help if you give me the request ID.

Hi @AmosHersch ,

Thanks so much for your assistance I really appreciate it.  Sorry I have no idea what I'm doing, so I've taken a while to try more things out and try and grasp what is happenning.

I've updated my authority url and still have the same problems.  You mentioned above;

If you still can't get it to work please see in the error response you get has request ID in the header

When I set _apiUrl = new Uri("https://api.powerbi.com/");
I get the following error:
{"error":{"code":"PowerBIEntityNotFound","pbi.error":{"code":"PowerBIEntityNotFound","parameters":{},"details":[],"exceptionCulprit":1}}}

request id = 'a1efd032-385d-4c02-99aa-0522a15a410e'

 

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

//note this was from a sample online??? no idea if its correct...

error response is empty....The process hangs for about 10 seconds and returns internal server error.

 

Am I correct in assuming my app has authenticated ok in AzureAd via the client-id, client-secret and app id?  authenticationResult is populated with a jwt token and expiration date.  I assume this came from azuread authentication process.

The process falls over when i try to then export the report via power BI api.

I'm so confused 😂

 

Hi @keegan ,

 

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

 

I think that either the report ID you're using doesn't exist, or the user/service principal which is used when calling the API doesn't have access to the workspace of the report.

You should add the user/service principal as a member to the workspace.

 

 

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.