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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
SoulsSama
Frequent Visitor

How to export reports generated by Power Bi on my website as an end user without using PowerBi app?

Hello, so I want to let users to export reports from the website as an end user, without using power Bi application. I already has everything set server side, I have PowerBi.API and NuGetPackage manager both installed and my reports are being generated on my website. I am following this code sample from Microsoft's documentation  , but after copying the sample code I got a lot of errors, I know out of context means that I need to import some needed libraries, since I am new to ASP and Power Bi in general, I have no idea what to import since visual studio not suggesting anything to import. Any Idea how to implement this? What am I missing?

Here is some screenshots from my code. The sample code below is what I am implementing from Microsoft documentation to use in my controller, please note that I cant share more than these since It's a source code for my employer.

Some screenshots and code here:

mstsc_QTh9601LRP.pngmstsc_LX6nrQOolr.pngmstsc_NfRjfuO28Y.png

 

 

 

 

 

 

 

  /* ********** Export reports functionality ********** */

    private async Task<string> PostExportRequest(Guid reportId,Guid groupId)
    {
        // For documentation purposes the export configuration is created in this method
        // Ordinarily, it would be created outside and passed in
        var paginatedReportExportConfiguration = new PaginatedReportExportConfiguration()
        {
            FormatSettings = new Dictionary<string, string>()
    {
        {"PageHeight", "14in"},
        {"PageWidth", "8.5in" },
        {"StartPage", "1"},
        {"EndPage", "4"},
    },
            ParameterValues = new List<ParameterValue>()
    {
        { new ParameterValue() {Name = "State", Value = "WA"} },
        { new ParameterValue() {Name = "City", Value = "Redmond"} },
    },
        };

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

        var export = await Client.Reports.ExportToFileInGroupAsync(groupId, reportId, exportRequest);

        // Save the export ID, you'll need it for polling and getting the exported file
        return export.Id;
    }

    private async Task<Export> PollExportRequest(
    Guid reportId,
    Guid groupId,
    string exportId /* Get from the ExportToAsync response */,
    int timeOutInMinutes,
    CancellationToken token)
{
    Export exportStatus = null;
    DateTime startTime = DateTime.UtcNow;
    const int secToMillisec = 1000;
    do
    {
        if (DateTime.UtcNow.Subtract(startTime).TotalMinutes > timeOutInMinutes || token.IsCancellationRequested)
        {
            // Error handling for timeout and cancellations
            return null;
        }

        var httpMessage = 
            await Client.Reports.GetExportToFileStatusInGroupWithHttpMessagesAsync(groupId, reportId, exportId);
            
        exportStatus = httpMessage.Body;
        if (exportStatus.Status == ExportState.Running || exportStatus.Status == ExportState.NotStarted)
        {
            // The recommended waiting time between polling requests can be found in the RetryAfter header
            // Note that this header is only populated when the status is either Running or NotStarted
            var retryAfter = httpMessage.Response.Headers.RetryAfter;
            var retryAfterInSec = retryAfter.Delta.Value.Seconds;

            await Task.Delay(retryAfterInSec * secToMillisec);
        }
    }
    // While not in a terminal state, keep polling
    while (exportStatus.Status != ExportState.Succeeded && exportStatus.Status != ExportState.Failed);

    return exportStatus;
}
    private async Task<ExportedFile> GetExportedFile(
        Guid reportId,
        Guid groupId,
        Export export /* Get from the GetExportStatusAsync response */)
    {
        if (export.Status == ExportState.Succeeded)
        {
            var httpMessage =
                await Client.Reports.GetFileOfExportToFileInGroupWithHttpMessagesAsync(groupId, reportId, export.Id);

            return new ExportedFile
            {
                FileStream = httpMessage.Body,
                ReportName = export.ReportName,
                FileExtension = export.ResourceFileExtension,
            };
        }

        return null;
    }

    public class ExportedFile
    {
        public Stream FileStream;
        public string ReportName;
        public string FileExtension;
    }

    private async Task<ExportedFile> ExportPaginatedReport(
        Guid reportId,
        Guid groupId,
        int pollingtimeOutInMinutes,
        CancellationToken token)
    {
        try
        {
            var exportId = await PostExportRequest(reportId, groupId);

            var export = await PollExportRequest(reportId, groupId, exportId, pollingtimeOutInMinutes, token);
            if (export == null || export.Status != ExportState.Succeeded)
            {
                // Error, failure in exporting the report
                return null;
            }

            return await GetExportedFile(reportId, groupId, export);
        }
        catch
        {
            // Error handling
            throw;
        }
    }

 

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Hello @v-chenwuz-msft
I actually found out the reason and solved it, it was because i was using PowerBi.APi v2 and I had to update to latest v 4.x , after that updating my code to match the new syntax fixed the errors, but then I found out that I  cant export reports at all as a free user with premium trial!, I get error 403 forbidden and FeatureNotAvailable error code which is as I read online in this forum is because I need to have a premium account, really disappointing that such a basic feature that works on PowerBi Application needs a premium license for the api to work.

View solution in original post

2 REPLIES 2
v-chenwuz-msft
Community Support
Community Support

Hi @SoulsSama ,

 

Is it possible to try to copy the code in the official article directly and modify some of the parameters and run.

 


Best Regards

Community Support Team _ chenwu zhu

Hello @v-chenwuz-msft
I actually found out the reason and solved it, it was because i was using PowerBi.APi v2 and I had to update to latest v 4.x , after that updating my code to match the new syntax fixed the errors, but then I found out that I  cant export reports at all as a free user with premium trial!, I get error 403 forbidden and FeatureNotAvailable error code which is as I read online in this forum is because I need to have a premium account, really disappointing that such a basic feature that works on PowerBi Application needs a premium license for the api to work.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.