4

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 >
Jefferson
  • 173
  • 2
  • 12
  • 32
  • Whats is the error? – Renato Leite Sep 22 '17 at 17:48
  • Unable to get property 'models' of undefined or null reference – Jefferson Sep 22 '17 at 18:03
  • I guess that probably you got this code in the demo project, however, looking in the Power BI community this looks like a address problem, the demo code can be trying to access an internal Microsoft address, what I suggest you to try is to download the Git code and to use like the git page is saying and not like the demo is doing. Look this reference, a guy has the same issue that you: http://community.powerbi.com/t5/Developer/PowerBI-Client-2-0-beta-testing/td-p/51504 – Renato Leite Sep 22 '17 at 18:36

4 Answers4

2

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);
    });
RBreuer
  • 1,371
  • 1
  • 7
  • 17
2

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;
Igor S
  • 951
  • 7
  • 19
1

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?
Qiniso
  • 2,587
  • 1
  • 24
  • 30
Yash Mochi
  • 769
  • 6
  • 15
  • 1
    Hi @Yash -I tried your code but I am still getting an error about undefined models. https://raw.githubusercontent.com/Microsoft/PowerBI- JavaScript/master/dist/powerbi.js was blocked due to mime type mismatch dashboard.html SCRIPT5007: Unable to get property 'models' of undefined or null reference dashboard.html (13,9) – Jefferson Oct 02 '17 at 14:30
1

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
double-beep
  • 5,031
  • 17
  • 33
  • 41
Mohan
  • 11
  • 1