1

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.

1
  • After trying to save the report via UI, I received error on UI Activity ID9130d6f6-16fb-4d10-b6c6-d7579648e081 Request ID25d20acf-68a1-06ea-5c02-60e44ac60b19 Correlation ID81f1ee36-a399-73c0-f32f-efa6c2e355e0 TimeThu Oct 24 2024 15:36:31 GMT+0300 (Eastern Europe Daylight Time) Service Version13.0.24488.44 Client Version2410.2.21363-train Cluster URIhttps://WABI-WEST-EUROPE-E-PRIMARY-redirect.analysis.windows.net Commented Oct 24, 2024 at 13:03

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. I was struggling for hours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.