Jefferson
Jefferson

Reputation: 93

PowerBi embed dashboard using JavaScript

I created a dashboard in PowerBi that I can load in a .cs page using the sample project but I like to try and use the JavaScript API. I tried using the project [GitHub Sample Project https://github.com/Microsoft/PowerBI-JavaScript] But I am getting error about the models is there a different function I would be using? I belive that I have all of the js library installed but the dashboard will not loaded in my html page

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.7.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/jquery-ui.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.3/es6-shim.js"></script>
    <script src="scripts/step_interact.js"></script>
    <script src="scripts/step_embed.js"></script>
    <script src="scripts/step_authorize.js"></script>
    <script src="/bower_components/powerbi-client/dist/powerbi.js"></script>
    <script>

    $(document).ready(function () {

        // Get models. models contains enums that can be used.
        var models = window['powerbi-client'].models;

        var embedConfiguration = {
            type: 'dashboard',
            id: 'dashboardID',
            embedUrl: 'https://app.powerbi.com/reportEmbed',
            tokenType: models.TokenType.Aad,
            accessToken: 'TokenString'
        };

        var $dashboardContainer = $('#embedContainer');
        var dashboard = powerbi.embed($dashboardContainer.get(0), embedConfiguration);
    });

    </script>
    </head >
            <body>
                <div id="embedContainer"></div>         
</body >
</html >

Upvotes: 4

Views: 16740

Answers (4)

Mohan
Mohan

Reputation: 11

Ensure that you have registered your exact URL as Redirect URL in "Power BI App Registration tool". Below code will work just fine with proper redirect URL set.

var models = window['powerbi-client'].models

Upvotes: 1

Yash Mochi
Yash Mochi

Reputation: 967

You can try this code for displaying PowerBI dashboard using Javascript. All, you will need is valid access token and dashboardId.

<html>
  <head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
  <script src="https://raw.githubusercontent.com/Microsoft/PowerBI-JavaScript/master/dist/powerbi.js"></script>
  </head>
  <body>
    <script type="text/javascript">
    window.onload = function () {
      var models = window['powerbi-client'].models;
      var embedConfiguration = {
        type: 'dashboard',
        accessToken: {{access token}},
        embedUrl: 'https://app.powerbi.com/dashboardEmbed?dashboardId={{dashboard id})'
      };  

      var $reportContainer = $('#dashboardContainer');
      var report = powerbi.embed($reportContainer.get(0), embedConfiguration);
    }
    </script>
    <div id="dashboardContainer"></div>
  </body>
</html>
How to get dashboard id or report id using group id in jquery/javascript?

Upvotes: 1

Igor S
Igor S

Reputation: 969

You need to change the model initialization to this:

import * as pbi from 'powerbi-client'


const models = pbi.models

instead of using this example:

var models = window['powerbi-client'].models;

Upvotes: 2

RBreuer
RBreuer

Reputation: 1391

A few things:

  1. From looking at powerbi.js file you're referencing to, try to change the following line: var models = window['powerbi-client'].models to var models = window.powerbi.models

  2. Make sure to use the correct embedURL, you're using /reportEmbed when you should be using /dashboardEmbed.

eventually, your code should look something like this:

$(document).ready(function () {

        // Get models. models contains enums that can be used.
        var models = powerbi.models; // or window.powerbi.models

        var embedConfiguration = {
            type: 'dashboard',
            id: 'dashboardID',
            embedUrl: 'https://app.powerbi.com/dashboardEmbed',
            tokenType: models.TokenType.Aad,
            accessToken: 'TokenString'
        };

        var $dashboardContainer = $('#embedContainer');
        var dashboard = powerbi.embed($dashboardContainer.get(0), embedConfiguration);
    });

Upvotes: 3

Related Questions