Display Power BI Report in SharePoint Online (using iFrame and Provider Hosted Apps)

This Power BI tutorial, we will discuss how to display Power BI report in SharePoint Online using iFrame as well as how to display Power BI report in SharePoint Online using provider hosted app or add-in.

Power BI is a great tool for data visualization. Many organizations using the tool for reporting purposes. Now showing Power BI reports in SharePoint online has its own trick. There are many ways to display Power BI reports to SharePoint online.

I will be sharing two major approaches and each approach pros and cons.

Below two approaches can be used based on your project security requirement.

  • Publish the Power BI Report and Display in Iframe
  • Provider Hosted App to display the report

Publish the Power BI Report and Display in Iframe in SharePoint Online

All reports need to be placed in Power BI online and then follow the below steps to display the report in SharePoint Online.

Publish the Report to the internet. When we publish Power BI report, automatically it will be published to the internet.

Get the Report URL once it’s published.

Add the content editor web part to SP page where you want to display the report.

Display the report in an iframe.

<iframe width="1100" height="600" src="https://app.powerbi.com/view?r=**************************************************************************************" frameborder="0" allowFullScreen="true"></iframe>

Cons of the Approach

The report will be displayed in SP Online without any additional authentication for Power BI.

View Page source can be used to find the report URL. It may be a security issue as the report URL will be published to the internet and accessible anonymously.

Provider Hosted App to display Power BI report in SharePoint Online

Now, we will discuss how to display Power BI reports in SharePoint online using Provider-hosted apps/add-in.

In the iFrame approach, there is security is a big concern as the report will be published on the internet.

There few steps we need to follow to display Power BI reports in SharePoint Online using Provider-hosted apps.

  • Provider-hosted MVC app will be created and hosted in Azure Website.
  • Register a client app with the Power BI App Registration Tool
  • Request Token will be generated run-time with the help of client ID of your AAD application
  • PowerBI API will be used to display the report in iframe provided by dashboard id, and report id.

So let’s talk in detail how to display Power BI reports in SharePoint Online using Provider Hosted Add-in.

The PowerBI report needs to be uploaded to the Power BI Portal.

We have to create a provider-hosted app to display the report.

Follow the below URL to register the App.
https://powerbi.microsoft.com/en-us/documentation/powerbi-developer-register-a-client-app/

Copy your provider hosted app SSL URL from visual studio project settings and the same needs to be registered in Azure AD power BI application settings.

Once you register the application in AD it will generate client id which will be used in provider hosted app to create request token.

Open index.cshtml file from your MVC provider hosted app.

Display Power BI Report in SharePoint Online

Place the below code and make sure you update client id (generated from Azure AD application registration), report id, tile id, reply url parameter in the code.

I have used a updateEmbedTile function to display the tile of your PowerBI dashboard and updateEmbedReport function to display the PowerBI report.

Publish and package your app for further to use in SharePoint Online site.

Add the webpart to the SharePoint page where you want to display the report.

@{
ViewBag.Title = "Home Page";
}

<div class="BIReports">
<div style="float:left; width:60%;">
<h3>Report With Drill Down </h3>
<iframe id="iFrameEmbedTile3" src="" height="700px" width="600px" frameborder="0" seamless></iframe>
</div>
<div style="float:left; width:40%;">
<h3>Dashborad with Tile </h3>
<iframe id="iFrameEmbedTile1" src="" height="500px" width="450px" frameborder="0" seamless style="float:right"></iframe>
</div>
@*<table>
<tr>
<td colspan="2">

</td>
</tr>
<tr>

<td><iframe id="iFrameEmbedTile1" src="" height="300px" width="300px" frameborder="0" seamless></iframe></td>
<!– Nombre total de likes–>
<td><iframe id="iFrameEmbedTile2" src="" height="300px" width="300px" frameborder="0" seamless></iframe></td>
</tr>
</table>*@
</div>
<script type="text/javascript" src="/Scripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript">

var token = null;

$(document).ready(function () {

if (urlParameterExtraction.queryStringParameters[‘access_token’] == null) { requestToken(); }
// Extract token from urlParameterExtraction object.
token = urlParameterExtraction.queryStringParameters[‘access_token’];

updateEmbedTile("iFrameEmbedTile1", "https://app.powerbi.com/embed?dashboardId=<provide dashboardId>", 500, 450);

updateEmbedReport("iFrameEmbedTile3", "https://app.powerbi.com/reportEmbed?reportId=<provide reportId>", 700, 600);
});

function updateEmbedTile(iFrameId, embedTileUrl, h, w) {
console.log("updateEmbedTile");
if ("" === embedTileUrl)
return;
iframe = document.getElementById(iFrameId);
iframe.src = embedTileUrl + "&width=" + w + "&height=" + h;
iframe.onload = function () { postActionLoadTile(iFrameId, h, w); }
}
function postActionLoadTile(iFrameId, h, w) {

accessToken = token;
if ("" === accessToken)
return;
var m = { action: "loadTile", accessToken: accessToken, height: h, width: w };
message = JSON.stringify(m);
iframe = document.getElementById(iFrameId);
iframe.contentWindow.postMessage(message, "*");;
}

function updateEmbedReport(iFrameId, embedReportUrl) {

if ("" === embedReportUrl)
return;

var iframe = document.getElementById(iFrameId);
iframe.src = embedReportUrl;
iframe.onload = function () { postActionLoadReport(iFrameId); }
}
function postActionLoadReport(iFrameId) {
console.log("postActionLoadReport");
accessToken = token;

if ("" === accessToken)
return;
var m = { action: "loadReport", accessToken: accessToken };
message = JSON.stringify(m);
iframe = document.getElementById(iFrameId);
iframe.contentWindow.postMessage(message, "*");
}

function requestToken() {
var clientId = ‘********-****-****-****-************’;
var replyUrl = ‘https://abc.azurewebsites.net’;
var resource = "https://analysis.windows.net/powerbi/api";
var authServer = ‘https://login.windows.net/common/oauth2/authorize?’;
var responseType = ‘token’;

var url = authServer +
"response_type=" + encodeURI(responseType) + "&" +
"client_id=" + encodeURI(clientId) + "&" +
"resource=" + encodeURI(resource) + "&" +
"redirect_uri=" + encodeURI(replyUrl);

window.location = url;
}

var urlParameterExtraction = new (function () {
function splitQueryString(queryStringFormattedString) {
var split = queryStringFormattedString.split(‘&’);
if (split == "") {
return {};
}

var results = {};
for (var i = 0; i < split.length; ++i) {
var p = split[i].split(‘=’, 2);
if (p.length == 1)
results[p[0]] = "";
else
results[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
}

return results;
}
this.queryStringParameters = splitQueryString(window.location.hash.substr(1));
})();
</script>

You may like following Power BI tutorials:

This tutorial, we learned how to display the Power BI dashboard in SharePoint Online using iFrame and Provider hosted app or add-in.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

Sambita Rath

I have 9 years of exprience into SharePoint implementation, architecture, Administrator, development and Training.Designing Information Architecture in SharePoint 2007, 2010,2013 and Office 365.

>