javascriptpowerbiazure-active-directorypowerbi-desktoppowerbi-embedded

Trouble when creating PowerBI report via JavaScript Client Library


I'm trying to create a new PowerBI report via JS library. I have a Microsoft Entra ID (Azure AD) Service Principal that assigned as admin to PowerBI workspace Permissions Also I have a dataset as a base for report creation. I created a simple html+js page with the following code:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Power BI Embedded Demo</title>
    <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
    <script type="text/javascript" language="javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript" language="javascript" src="https://rawgit.com/Microsoft/PowerBI-JavaScript/master/dist/powerbi.min.js"></script>
</head>
<body>
    <h1>Power BI Embedded Demo</h1>
    <button type="button" id="save-btn">SAVE</button>
    <div id="reportContainer" style="width: 80%; height: 600px;"></div>
</body>
<script>
    $(document).ready(function () {
        var models = window['powerbi-client'].models;
        console.log(models);
        
        var embedConfiguration = {
            type: 'create',   // Supported types: report, dashboard, tile, visual, qna, paginated report and create
            datasetId: '838f7f0b-9ad6-4e9a-8924-cadb688f8cb8',
            groupId: '75c3eaf3-74cb-454b-aa86-d5480e66fded',
            embedUrl: 'My embed URL',
            accessToken: 'Dataset  access token',
            tokenType: models.TokenType.Embed
        };

        var $reportContainer = $('#reportContainer');
        var report = powerbi.createReport($reportContainer.get(0), embedConfiguration);
        console.log("Отчёт создан:", report);

        report.on("loaded", function() {
            console.log("Report loaded");
        });
        
        report.on("error", function(event) {
            console.error("Error while embedding report", event.detail);
        });

        let saveAsParameters = {
            name: "TESTREPORT"
        };
        
        report.saveAs(saveAsParameters).then(function (savedReport) {
            console.log("Saved!");
            console.log("Report info", savedReport);
        }).catch(function (error) {
            console.error("Error while saving report:", error);
            console.error("Error details:", error.body);
        });;

        let isReportSaved = report.isSaved().then(function (result) {
            console.log("Report saved:", result);
        }).catch(function (error) {
            console.error("Trouble saving report:", error);
        });;
    });
</script>
</html>

In the result I was able to create report, but not to save it

Created but not saved report

As I saw from console, saveAs code hasn't been performed, but when I tried to click on save as button on the PowerBI embedded UI, (Top left corner: File -> Save as (Файл -> Сохранить как)) I received an error: Error in console

To get embed token, I authenticated via Service Principal and sent request to the https://api.powerbi.com/v1.0/myorg/GenerateToken

To get createReportEmbedURL I authenticated via Service Principal and sent request to the https://api.powerbi.com/v1.0/myorg/groups/{my workspace}/datasets/{my dataset}

I'm expecting to have created report.


Solution

  • So after trying tones of things I found a solution. Documentation says that I need embed token with access to dataset (semantic model in app.powerbi.com), but I does not say anything about the fact that I need to pass not only dataset id, but also workspace/group Id. https://learn.microsoft.com/en-us/rest/api/power-bi/embed-token/generate-token Here is the url: https://api.powerbi.com/v1.0/myorg/GenerateToken Here is the example body:

    {
      "datasets": [
        {
          "id": "00000000-0000-0000-0000-000000000000"
        }
      ],
      "targetWorkspaces": [
        {
            "id": "00000000-0000-0000-0000-000000000000"
        }
      ]
    
    }
    

    I had performed authentication under service principal that was owned by master user account with Fabric Administrator role. Hope it will help for somebody.

    Also you can try to use older version of PowerBI Rest API and call another endpoint to get the access token. https://learn.microsoft.com/en-us/rest/api/power-bi/embed-token/reports-generate-token-for-create-in-group