示例#1
0
        /////// Export sample ///////
        private async Task <string> PostExportRequest(
            PowerBIClient client,
            Guid reportId,
            Guid groupId,
            FileFormat format,
            IList <string> pageNames = null /* Get the page names from the GetPages API */)
        {
            var powerBIReportExportConfiguration = new PowerBIReportExportConfiguration
            {
                Settings = new ExportReportSettings
                {
                    Locale = "en-us",
                },
                // Note that page names differ from the page display names.
                // To get the page names use the GetPages API.
                Pages = pageNames?.Select(pn => new ExportReportPage(pageName: pn)).ToList(),
            };
            var exportRequest = new ExportReportRequest
            {
                Format = format,
                PowerBIReportConfiguration = powerBIReportExportConfiguration,
            };
            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);
        }
示例#2
0
        /// <summary>
        /// Exports the file.
        /// </summary>
        /// <typeparam name="T">The Type</typeparam>
        /// <param name="request">The request.</param>
        /// <param name="response">The data.</param>
        /// <returns>Http Response Message</returns>
        protected virtual HttpResponseMessage ExportFile <T>(ExportReportRequest request, T response)
        {
            switch (typeof(T).Name)
            {
            case "DataTable":
                var dataTable = response as DataTable;

                switch (request.Format.ToLower())
                {
                case "pdf":
                    return(new HttpResponseMessage(HttpStatusCode.OK)
                           .SendPDF(Xhr.App.One.Core.FileExportExtensions.ToPdfByteArray(dataTable), request.FileName));

                case "excel":
                    return(new HttpResponseMessage(HttpStatusCode.OK)
                           .SendExcel(Xhr.App.One.Core.FileExportExtensions.ToExcelByteArray(dataTable, request.SheetName ?? request.FileName, "", request.SheetHeader), request.FileName));

                default: break;
                }

                break;

            case "String":
                var st = response as string;

                switch (request.Format.ToLower())
                {
                case "text":
                    return(new HttpResponseMessage(HttpStatusCode.OK)
                           .SendText(st, request.FileName));

                case "pdfform":
                    return(new HttpResponseMessage(HttpStatusCode.OK)
                           .SendPDF(Xhr.App.One.Core.FileExportExtensions.ToPdfForm(st, request.ShowPageLabels, request.PageOrientation), request.FileName));

                default: break;
                }

                break;

            case "Byte[]":
                var byteArr = response as byte[];

                switch (request.Format.ToLower())
                {
                case "excel":
                    return(new HttpResponseMessage(HttpStatusCode.OK)
                           .SendExcel(byteArr, request.FileName));

                default: break;
                }

                break;

            default: break;
            }

            return(new HttpResponseMessage(HttpStatusCode.NotFound));
        }
        /// <summary>
        /// Initialize export request for report
        /// </summary>
        /// <returns>Id of Export request</returns>
        private async Task <string> InitExportRequest(string pageName, FileFormat fileFormat, string pageState = null, string username = null, string role = null)
        {
            PageBookmark pageBookmark = null;

            if (!string.IsNullOrWhiteSpace(pageState))
            {
                // To export report page with current bookmark
                pageBookmark = new PageBookmark(null, pageState);
            }

            // Get Power BI report object
            var pbiReport = pbiClient.Reports.GetReportInGroup(workspaceId, reportId);

            // Create effective identity for current user
            List <EffectiveIdentity> identities = null;

            if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(role))
            {
                identities = new List <EffectiveIdentity> {
                    new EffectiveIdentity(username: username, roles: new List <string> {
                        role
                    }, datasets: new List <string> {
                        pbiReport.DatasetId
                    })
                };
            }

            var powerBIReportExportConfiguration = new PowerBIReportExportConfiguration
            {
                Settings = new ExportReportSettings
                {
                    Locale = Constant.DefaultLocale
                },

                // Initialize list of pages along with their state to be exported
                Pages = new List <ExportReportPage>()
                {
                    new ExportReportPage(pageName, pageBookmark)
                },

                Identities = identities
            };

            var exportRequest = new ExportReportRequest
            {
                Format = fileFormat,
                PowerBIReportConfiguration = powerBIReportExportConfiguration,
            };

            // Initiate export process
            var export = await pbiClient.Reports.ExportToFileInGroupAsync(workspaceId, reportId, exportRequest);

            return(export.Id);
        }
示例#4
0
        public string ExportReport(string reportUri, ExportFormatTypes exportFormatType = ExportFormatTypes.csv)
        {
            CheckAuthentication();
            var executeUri = ExecuteReport(reportUri);

            var url = Url.Combine(Config.Url, Constants.EXPORT_EXECUTOR);

            var payload = new ExportReportRequest
            {
                result_req = new ResultRequest(exportFormatType)
                {
                    Report = executeUri
                }
            };
            var response       = PostRequest(url, payload);
            var exportResponse = JsonConvert.DeserializeObject(response, typeof(UriResponse)) as UriResponse;

            return(exportResponse.Uri);
        }
示例#5
0
        public static void ExportVisual(Guid WorkspaceId, Guid ReportId, string PageName, string VisualName)
        {
            PowerBIClient pbiClient = TokenManager.GetPowerBiClient(requiredScopes);

            var exportRequest = new ExportReportRequest {
                Format = FileFormat.PDF,
                PowerBIReportConfiguration = new PowerBIReportExportConfiguration {
                    Pages = new List <ExportReportPage>()
                    {
                        new ExportReportPage {
                            PageName   = PageName,
                            VisualName = VisualName
                        }
                    }
                }
            };

            Export export = pbiClient.Reports.ExportToFileInGroup(WorkspaceId, ReportId, exportRequest);

            string exportId = export.Id;

            do
            {
                System.Threading.Thread.Sleep(5000);
                export = pbiClient.Reports.GetExportToFileStatus(ReportId, exportId);
                Console.WriteLine("Getting export status - " + export.PercentComplete.ToString() + "% complete");
            } while (export.Status != ExportState.Succeeded && export.Status != ExportState.Failed);

            if (export.Status == ExportState.Failed)
            {
                Console.WriteLine("Export failed!");
            }

            if (export.Status == ExportState.Succeeded)
            {
                string fileName = @"c:\DevCamp\Visual1.pdf";
                Console.WriteLine("Saving exported file to " + fileName);
                var        exportStream = pbiClient.Reports.GetFileOfExportToFile(WorkspaceId, ReportId, exportId);
                FileStream fileStream   = File.Create(fileName);
                exportStream.CopyTo(fileStream);
                fileStream.Close();
            }
        }
示例#6
0
 /// <summary>
 /// Exports the specified report from the specified workspace to the requested
 /// format: PPTX/PDF/PNG.
 /// </summary>
 /// <remarks>
 /// &lt;br/&gt;**Required scope**: Report.ReadWrite.All or Report.Read.All
 /// &lt;br/&gt;To set the permissions scope, see [Register an
 /// app](https://docs.microsoft.com/power-bi/developer/register-app).
 /// &lt;h2&gt;Restrictions&lt;/h2&gt;Currently only export of a single page is
 /// supported.&lt;br/&gt;
 /// </remarks>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='groupId'>
 /// The group id
 /// </param>
 /// <param name='reportId'>
 /// The report id
 /// </param>
 /// <param name='requestParameters'>
 /// Export to file request parameters
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <Export> ExportToFileAsync(this IReportsOperations operations, Guid groupId, Guid reportId, ExportReportRequest requestParameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.ExportToFileInGroupWithHttpMessagesAsync(groupId, reportId, requestParameters, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
示例#7
0
 /// <summary>
 /// Exports the specified report from the specified workspace to the requested
 /// format: PPTX/PDF/PNG.
 /// </summary>
 /// <remarks>
 /// &lt;br/&gt;**Required scope**: Report.ReadWrite.All or Report.Read.All
 /// &lt;br/&gt;To set the permissions scope, see [Register an
 /// app](https://docs.microsoft.com/power-bi/developer/register-app).
 /// &lt;h2&gt;Restrictions&lt;/h2&gt;Currently only export of a single page is
 /// supported.&lt;br/&gt;
 /// </remarks>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='groupId'>
 /// The group id
 /// </param>
 /// <param name='reportId'>
 /// The report id
 /// </param>
 /// <param name='requestParameters'>
 /// Export to file request parameters
 /// </param>
 public static Export ExportToFile(this IReportsOperations operations, Guid groupId, Guid reportId, ExportReportRequest requestParameters)
 {
     return(operations.ExportToFileAsync(groupId, reportId, requestParameters).GetAwaiter().GetResult());
 }
示例#8
0
        public string ExportReport(string reportUri, ExportFormatTypes exportFormatType = ExportFormatTypes.csv)
        {
            CheckAuthentication();
            var executeUri = ExecuteReport(reportUri);

            var url = Url.Combine(Config.ServiceUrl, Constants.EXPORT_EXECUTOR);

            var payload = new ExportReportRequest {
                result_req = new ResultRequest(exportFormatType) {
                    Report = executeUri
                }
            };
            var response = JsonPostRequest(url, payload);
            var exportResponse = JsonConvert.DeserializeObject(response, typeof(UriResponse)) as UriResponse;
            return exportResponse.Uri;
        }