skip to Main Content

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

enter image description here

enter image description here
enter image description here
enter image description here

        /* ********** 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;
        }
    }

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution, I was using an older version of PowerBi.Api, i was using V2 and when I updated it to V4 and fixed my code to match the new version it worked with no errors, hopefully this will help anyone who face the same problem in the future!


  2. Looking at the errors from your screenshots, it looks like you didn’t added the Power BI SDK (also see Power BI Embedded libraries for .NET). You must install Microsoft.PowerBI.Api NuGetPackage. One way to do that is to run the following command in the package manager in Visual Studio:

    Install-Package Microsoft.PowerBI.Api
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search