0
votes

Power BI report can be exported from https://app.powerbi.com/ page using inbuilt Export feature.

I have a requirement to provide same functionality from within SharePoint Online page, where this report is embedded. Please let me know the steps.

1

1 Answers

1
votes

You can use Export To File / Export To File In Group API.

  1. When the Export-To-File API is called, it triggers an export job.

  2. After triggering the export job, you can use the Polling API (Get Export To File Status / Get Export To File Status In Group) to track the job until it is complete.

  3. When the export job is complete, the Polling API call returns a Power BI URL for getting the file (the URL is available for 24 hours). You can also download it by calling Get File Of Export To File / Get File Of Export To File In Group API.

The API supports concurrent export job requests. The number of jobs you can run at the same time, depends on the SKU your report resides on, as detailed in this table (i.e. it requires a dedicated capacity - Power BI Premium or Power BI Embedded).

For example, here is how you can use the API with PowerShell code:

Import-Module MicrosoftPowerBIMgmt
$username = "[email protected]" 
$password = "SuperStrongPassword" | ConvertTo-SecureString -asPlainText -Force
$groupId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$reportId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$format = "PDF" # or PNG, or PPTX. For paginated reports can be CSV, DOCX, IMAGE (i.e. page definition plus file format - BMP, EMF, etc.), MHTML, XLSX or XML
$saveToFolder = "D:\"

$credential = New-Object System.Management.Automation.PSCredential($username, $password)
Connect-PowerBIServiceAccount -Credential $credential
$settings = @{ includeHiddenPages = $false; locale = "en-us" }
$powerBIReportConfiguration = @{ settings = $settings }
$export_body = @{ format = $format; powerBIReportConfiguration = $powerBIReportConfiguration }
$export_response = Invoke-PowerBIRestMethod -Url "https://api.powerbi.com/v1.0/myorg/groups/$groupId/reports/$reportId/ExportTo" -Method Post -Body ($export_body | ConvertTo-Json)
$export_response_json = ConvertFrom-Json $export_response
$exportId = $export_response_json.id

Write-Output "Polling export status..."
$maxPollCount = 500
$exportSucceeded = $false
do
{
    $maxPollCount = $maxPollCount - 1
    Start-Sleep -Seconds 5
    $status_response = Invoke-PowerBIRestMethod -Url "https://api.powerbi.com/v1.0/myorg/groups/$groupId/reports/$reportId/exports/$exportId" -Method Get
    $status_response_json = ConvertFrom-Json $status_response
    $status = $status_response_json.status
    $percentComplete = $status_response_json.percentComplete
    Write-Output "Status: $status, percent complete: $percentComplete (retries left: $maxPollCount)"
    if ($status -eq "Succeeded")
    {
        $exportSucceeded = $true
        $resourceLocation = $status_response_json.resourceLocation
        $reportName = $status_response_json.reportName
        $resourceFileExtension = $status_response_json.resourceFileExtension
        $outFile = [IO.Path]::Combine($saveToFolder, $reportName + $resourceFileExtension)
    }
}
until($exportSucceeded -or $maxPollCount -le 0)

Write-Output "Downloading export..."
$download_response = Invoke-PowerBIRestMethod -Url $resourceLocation -Method Get -OutFile $outFile
Write-Output "Download completed."
Disconnect-PowerBIServiceAccount