示例#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);
        }
        /// <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);
        }