Alexandru Stefan
Alexandru Stefan

Reputation: 655

Export current PowerBI screen to pptx

I have a web app that shows embedded PowerBI reports. Recently, with help of this link, I've added export of the PowerBI report to PowerPoint. Everything works fine, except the fact that the export is not taking into account at what level of detail the user was looking at. (e.g. he opened the report, but changed the selected tab and went two levels deep from what is shown by default).

I think that this can be addressed with Bookmark info, more details here, I've managed to get the bookmark info, but I can't find any info on how to make the report export take account of this bookmark info.

Do you guys have any link or hint how this can be achieved? Thank you!

LE with code:

Server code(few approaches and their result):

  1. With providing DefaultBookmark I get an empty PowerPoint file.

         var powerBIReportExportConfiguration = new PowerBIReportExportConfiguration
         {
             Settings = new ExportReportSettings
             {
                 Locale = "en-us",
             },
    
             Identities = new[] { ei },
             DefaultBookmark = new PageBookmark(name: bookmarkName, state: reportState),
         };
    
  1. With providing Pages with a PageBookmark based on bookmarkState I get the following error: {"error":{"code":"InvalidRequest","message":"Export report with page names which does not exist in the report is not supported"}}

         var powerBIReportExportConfiguration = new PowerBIReportExportConfiguration
         {
             Settings = new ExportReportSettings
             {
                 Locale = "en-us",
             },
    
             Identities = new[] { ei },
             Pages = new[] { new ExportReportPage(bookmark: new PageBookmark(state: bookmarkState)) },
         };
    
  2. With providing Pages with a PageBookmark based on bookmarkName I get the following error: {"error":{"code":"InvalidRequest","message":"Export report with page names which does not exist in the report is not supported"}}. Same as for the second approach.

         var powerBIReportExportConfiguration = new PowerBIReportExportConfiguration
         {
             Settings = new ExportReportSettings
             {
                 Locale = "en-us",
             },
    
             Identities = new[] { ei },
             Pages = new[] { new ExportReportPage(bookmark: new PageBookmark(name: bookmarkName)) },
         };
    
  3. Same bad request error if I provide both name and bookmark in the ExportReportPage bookmark.

Client code:

async reportBookmark(): Promise<models.IReportBookmark> {
return this.report.bookmarksManager
  .capture()
  .then(value => {
    return value;
  })
  .catch(() => {
    return null;
  });
}

Upvotes: 0

Views: 1634

Answers (3)

Bhushan Mahajan
Bhushan Mahajan

Reputation: 13

    public   void ExportReport(Guid WorkspaceId, Guid ReportId, string ExportName, FileFormat ExportFileFormat , PowerBIClient pbiClient , Report pbiReport , string EmbdToken , Pages? PageListInReport )
    {

        Console.WriteLine("==============================================");
        try
        {
            // 1
            EffectiveIdentity ei = new EffectiveIdentity
            {
                Username = appid,
                Roles = new List<string> { rolecode },
                Datasets = new List<string> { pbiReport.DatasetId },
                IdentityBlob = new IdentityBlob(EmbdToken),
                Reports = new List<string> { pbiReport.Id.ToString() }
            };

            List<EffectiveIdentity> leffid = new List<EffectiveIdentity> { ei };


            //2
            List<ExportReportPage> Pagese = new List<ExportReportPage>();

            if (PageListInReport != null)
            {

                foreach (var item in PageListInReport.Value)
                {
                    ExportReportPage pg = new ExportReportPage();
                    pg.PageName = item.Name;
                    Pagese.Add(pg);
                }

            }



            //3
            // create export report request
            var exportRequest = new ExportReportRequest
            {
                Format = ExportFileFormat,

                PowerBIReportConfiguration = new PowerBIReportExportConfiguration()
                {
                    Settings = new ExportReportSettings
                    {
                        Locale = "en-us",
                        IncludeHiddenPages = true
                    },
                    Identities = leffid,

                     
                }

            };


            //4
            // call ExportToFileInGroup to start async export job
            Export export = pbiClient.Reports.ExportToFileInGroup(WorkspaceId, ReportId, exportRequest);

            string exportId = export.Id;

            do
            {
                // poll every 5 seconds to see if export job has completed
                System.Threading.Thread.Sleep(5000);
                export = pbiClient.Reports.GetExportToFileStatusInGroup(WorkspaceId, ReportId, exportId);
                Console.WriteLine("Waiting");
                // continue to poll until export job status is equal to Suceeded or Failed
            } while (export.Status != ExportState.Succeeded && export.Status != ExportState.Failed);

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

            if (export.Status == ExportState.Succeeded)
            {
                // retreive file name extension from export object to construct file name
                string FileName = ExportName + export.ResourceFileExtension.ToLower();
                string FilePath = filepath + FileName;

                // get export stream with file
                var exportStream = pbiClient.Reports.GetFileOfExportToFileInGroup(WorkspaceId, ReportId, exportId);

                // save exported file stream to local file
                FileStream fileStream = File.Create(FilePath);
                exportStream.CopyTo(fileStream);
                fileStream.Close();

              
            }

        }
        catch (Exception ex )
        {
            Console.WriteLine(ex.ToString());

        }
         
      
    }

Upvotes: 0

Alexandru Stefan
Alexandru Stefan

Reputation: 655

After quite a break and some help I realised that the problem was between my web app and my api. I was sending the bookmarkState via the QueryParameters in the requests and some characters were changed or removed. Sending it via the body worked perfectly. Small mention, this is how the export configuration looked in the end:

 var powerBIReportExportConfiguration = new PowerBIReportExportConfiguration
 {
     Settings = new ExportReportSettings
     {
         Locale = "en-us",
     },

     Identities = new[] { ei },
     DefaultBookmark = new PageBookmark(state: reportState),
 };

Upvotes: 2

Andrey Nikolov
Andrey Nikolov

Reputation: 13460

Export To File In Group accepts pageBookmark to be applied on specific page and defaultBookmark, to be applied on all pages which don't have a specific bookmark. See the Bookmarks section in the link that you provided:

You can use the exportToFile API to programmatically export a report in a specific state, after applying filters to it. This is done using Bookmarks capabilities. To export a report using bookmarks, use the bookmarks JavaScript API.

For example, you can use the bookmark's capturedBookmark.state method to capture the changes a specific user made to a report, and then export it in its current state.

Personal bookmarks and persistent filters are not supported.

So you should not apply a bookmark on load, but capture the current state and pass it as a page bookmark when calling the export API.

Upvotes: 0

Related Questions