在 Power BI 工作区集合中基于数据集创建新报表Create a new report from a dataset in Power BI Workspace Collections

现在,可以基于自有应用程序中的数据集创建 Power BI 工作区集合报表。Power BI Workspace Collection reports can now be created from a dataset in your own application.

重要

Power BI 工作区集合已弃用,到 2018 年 6 月 或合同指示时可用。Power BI Workspace Collections is deprecated and is available until June 2018 or when your contract indicates. 建议你规划到 Power BI Embedded 的迁移以避免应用程序中断。You are encouraged to plan your migration to Power BI Embedded to avoid interruption in your application. 有关如何将数据迁移到 Power BI Embedded 的信息,请参阅如何将 Power BI 工作区集合内容迁移到 Power BI EmbeddedFor information on how to migrate your data to Power BI Embedded, see How to migrate Power BI Workspace Collections content to Power BI Embedded.

身份验证方法类似于嵌入报表时的身份验证方法。The authentication method is similar to that of embedding a report. 该方法基于特定于数据集的访问令牌。It is based on access tokens that are specific to a dataset. 用于 PowerBI.com 的令牌是由 Azure Active Directory (AAD) 颁发的。Tokens used for PowerBI.com are issued by Azure Active Directory (AAD). Power BI 工作区集合令牌是由你自己的应用程序颁发的。Power BI Workspace Collection tokens are issued by your own application.

创建嵌入报表时,颁发的令牌用于特定的数据集。When creating an Embedded report, the tokens issued are for a specific dataset. 令牌应与同一元素上的嵌入 URL 相关联,确保每个元素都有一个的唯一令牌。Tokens should be associated with the embed URL on the same element to ensure each has a unique token. 若要创建嵌入式报表,必须在访问令牌中提供 Dataset.Read 和 Workspace.Report.Create 作用域。In order to create an Embedded report, Dataset.Read and Workspace.Report.Create scopes must be provided in the access token.

创建所需的访问令牌用于创建新报表Create access token needed to create new report

Power BI 工作区集合使用一个嵌入令牌,这是一个 HMAC 已签名 JSON Web 令牌。Power BI Workspace Collections use an embed token, which is HMAC signed JSON Web Tokens. 已使用 Power BI 工作区集合中的访问密钥对令牌进行了签名。The tokens are signed with the access key from your Power BI Workspace Collection. 默认情况下,嵌入令牌用于提供对要嵌入到应用程序的报表的只读访问权限。Embed tokens, by default, are used to provide read-only access to a report to embed into an application. 嵌入令牌是针对特定报表颁发的,应该与嵌入 URL 相关联。Embed tokens are issued for a specific report and should be associated with an embed URL.

应在服务器上创建访问令牌,因为要使用访问密钥对令牌进行签名/加密。Access tokens should be created on the server as the access keys are used to sign/encrypt the tokens. 有关如何创建访问令牌的信息,请参阅通过 Power BI 工作区集合进行身份验证和授权For information on how to create an access token, see Authenticating and authorizing with Power BI Workspace Collections. 此外,还可以查看 CreateReportEmbedToken 方法。You can also review the CreateReportEmbedToken method. 以下示例演示了如何使用用于 Power BI 的 .NET SDK。Here is an example of what this would look like using the .NET SDK for Power BI.

在此示例中,我们已有要基于其创建新报表的数据集 ID。In this example, we have our dataset ID that we want to create the new report on. 还需要添加 Dataset.Read 和 Workspace.Report.Create 的作用域。We also need to add the scopes for Dataset.Read and Workspace.Report.Create.

PowerBIToken 类要求安装 Power BI Core NuGut 包The PowerBIToken class requires that you install the Power BI Core NuGut Package.

安装 NuGet 包NuGet package install

Install-Package Microsoft.PowerBI.Core

C# 代码C# code

using Microsoft.PowerBI.Security;

// rlsUsername and roles are optional
string scopes = "Dataset.Read Workspace.Report.Create";
PowerBIToken embedToken = PowerBIToken.CreateReportEmbedTokenForCreation(workspaceCollectionName, workspaceId, datasetId, null, null, scopes);

var token = embedToken.Generate("{access key}");

创建新的空白报表Create a new blank report

若要创建新报表,应提供 create 配置。In order to create a new report, the create configuration should be provided. 此配置应包含访问令牌、embedURL,以及用于创建报表的 datasetID。This should include the access token, the embedURL and the datasetID that we want to create the report against. 这就需要安装 Power BI JavaScript NuGet 包This requires that you install the nuget Power BI JavaScript package. embedUrl 将只是 https://embedded.powerbi.cn/appTokenReportEmbed。The embedUrl will just be https://embedded.powerbi.cn/appTokenReportEmbed.

备注

可以使用 JavaScript 报表嵌入示例测试功能。You can use the JavaScript Report Embed Sample to test functionality. 我们还提供了适用于不同操作的代码示例。It also gives code examples for the different operations that are available.

安装 NuGet 包NuGet package install

Install-Package Microsoft.PowerBI.JavaScript

JavaScript 代码JavaScript code

<div id="reportContainer"></div>

<script>  
var embedCreateConfiguration = {
        accessToken: 'eyJ0eXAiO...Qron7qYpY9MI',
        embedUrl: 'https://embedded.powerbi.cn/appTokenReportEmbed',
        datasetId: '5dac7a4a-4452-46b3-99f6-a25915e0fe55',
    };
    
    // Grab the reference to the div HTML element that will host the report
    var reportContainer = $('#reportContainer')[0];

    // Create report
    var report = powerbi.createReport(reportContainer, embedCreateConfiguration);
</script>    

调用 powerbi.createReport() 会在 div 元素中创建一个以编辑模式显示的空白画布。Calling powerbi.createReport() makes a blank canvas in edit mode appear within the div element.

新建空白报表

保存新报表Save new reports

在调用“另存为”操作之前,不会创建报表。The report is not created until you call the save as operation. 可以通过文件菜单或 JavaScript 完成此操作。This can be done from file menu or from JavaScript.

 // Get a reference to the embedded report.
    report = powerbi.get(reportContainer);
    
    var saveAsParameters = {
        name: "newReport"
    };

    // SaveAs report
    report.saveAs(saveAsParameters);

重要

只有在调用“另存为”之后,才创建新报表。A new report is created only after save as is called. 保存后,画布仍以编辑模式而不是报表模式显示数据集。After you save, the canvas will still show the dataset in edit mode and not the report. 需要重新加载新报表,就像使用其他任何报表时一样。You need to reload the new report like you would any other report.

文件菜单 - 另存为

加载新报表Load the new report

如果要与新报表交互,需要像应用程序嵌入常规报表一样来嵌入新报表,这意味着,必须专门针对新报表颁发新令牌,并调用 embed 方法。In order to interact with the new report you need to embed it in the same way the application embeds a regular report, meaning, a new token must be issued specifically for the new report and then call the embed method.

<div id="reportContainer"></div>
<script>  
var embedConfiguration = {
        accessToken: 'eyJ0eXAiO...Qron7qYpY9MJ',
        embedUrl: 'https://embedded.powerbi.cn/appTokenReportEmbed',
        reportId: '5dac7a4a-4452-46b3-99f6-a25915e0fe54',
    };
    
    // Grab the reference to the div HTML element that will host the report
    var reportContainer = $('#reportContainer')[0];

    // Embed report
    var report = powerbi.embed(reportContainer, embedConfiguration);
</script>    

使用“saved”事件自动保存和加载新报表Automate save and load of a new report using the "saved" event

要自动执行“另存为”过程,并加载新报表,可以使用“saved”事件。In order to automate the process of "save as" and then loading the new report, you can make use of the "saved" Event. 当保存操作已完成并返回包含新 reportId、报表名称、旧 reportId(如果有)的 Json 对象,并且操作是 saveAs 或 save 时,将激发此事件。This event is fired when the save operation is complete and it returns a Json object containing the new reportId, report name, the old reportId (if there was one) and if the operation was saveAs or save.

{
  "reportObjectId": "5dac7a4a-4452-46b3-99f6-a25915e0fe54",
  "reportName": "newReport",
  "saveAs": true,
  "originalReportObjectId": null
}

要自动执行该过程,可以侦听“saved”事件,提取新的 reportId,创建新令牌,并使用新令牌嵌入新报表。To Automate the process you can listen on the "saved" event, take the new reportId, create the new token, and embed the new report with it.

<div id="reportContainer"></div>
<script>    
var embedCreateConfiguration = {
        accessToken: 'eyJ0eXAiO...Qron7qYpY9MI',
        embedUrl: 'https://embedded.powerbi.cn/appTokenReportEmbed',
        datasetId: '5dac7a4a-4452-46b3-99f6-a25915e0fe55',
    };
    
    // Grab the reference to the div HTML element that will host the report
    var reportContainer = $('#reportContainer')[0];

    // Create report
    var report = powerbi.createReport(reportContainer, embedCreateConfiguration);


   var saveAsParameters = {
        name: "newReport"
    };

    // SaveAs report
    report.saveAs(saveAsParameters);

    // report.on will add an event handler which prints to Log window.
    report.on("saved", function(event) {
        
         // get new Token
         var newReportId =  event.detail.reportObjectId;

        // create new Token. This is a function that the application should provide
        var newToken = createAccessToken(newReportId,scopes /*provide the wanted scopes*/);
        
        
    var embedConfiguration = {
        accessToken: newToken ,
        embedUrl: 'https://embedded.powerbi.cn/appTokenReportEmbed',
        reportId: newReportId,
    };

    // Embed report
    var report = powerbi.embed(reportContainer, embedConfiguration);
       
   // report.off removes a given event handler if it exists.
   report.off("saved");
    });
</script>    

另请参阅See also

示例入门Get started with sample
保存报表Save reports
嵌入报表Embed a report
在 Power BI 工作区集合中进行身份验证和授权Authenticating and authorizing in Power BI Workspace Collections
Power BI DesktopPower BI Desktop
JavaScript 嵌入示例JavaScript Embed Sample
Power BI Core NuGut 包Power BI Core NuGut Package
Power BI JavaScript 包Power BI JavaScript package

有更多问题?More questions? 试用 Power BI 社区Try the Power BI Community