简体   繁体   English

从 SharePoint Online 导出 Power BI 报告

[英]Export Power BI Report from SharePoint Online

Power BI report can be exported from https://app.powerbi.com/ page using inbuilt Export feature. Power BI 报告可以使用内置的导出功能从https://app.powerbi.com/页面导出。

I have a requirement to provide same functionality from within SharePoint Online page, where this report is embedded.我需要从嵌入此报告的 SharePoint Online 页面中提供相同的功能。 Please let me know the steps.请让我知道步骤。

You can use Export To File / Export To File In Group API.您可以使用“ 导出到文件” /“ 导出到文件组API”。

  1. When the Export-To-File API is called, it triggers an export job.调用导出到文件 API 时,它会触发导出作业。

  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.触发导出作业后,您可以使用轮询 API( 获取导出到文件状态/ 获取导出到文件状态在组中)来跟踪作业,直到它完成。

  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).导出作业完成后,轮询 API 调用将返回用于获取文件的 Power BI URL(该 URL 可在 24 小时内使用)。 You can also download it by calling Get File Of Export To File / Get File Of Export To File In Group API.您也可以通过调用Get File Of Export To File / Get File Of Export To File In Group API 来下载它。

The API supports concurrent export job requests. API 支持并发导出作业请求。 The number of jobs you can run at the same time, depends on the SKU your report resides on, as detailed in this table (ie it requires a dedicated capacity - Power BI Premium or Power BI Embedded).您可以同时运行的作业数量取决于您的报告所在的 SKU, 如此表中详述(即它需要专用容量 - Power BI Premium 或 Power BI Embedded)。

For example, here is how you can use the API with PowerShell code:例如,您可以通过以下方式将 API 与 PowerShell 代码结合使用:

Import-Module MicrosoftPowerBIMgmt
$username = "account@example.com" 
$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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM