Forum Discussion
Is it possible to Embedded Powers BI Reports into a WinForms application?
- 10 years ago
Thank you!
Do you know some project example?
- 10 years ago
I know someone did it before but I am not aware of any samples,
Can i please have an example of wat is needed to view a power bi report from a vs 2015 c# winform application?
Hi,
as you know there are changes going on, I have not yet migrated to the new Power BI service.
Not clear what have you tried so far. But there are 2 main options
a) if your report contains no sensitive data - then you can use iframe (and you can get it into winform using WebControl btw.)
https://powerbi.microsoft.com/en-us/guided-learning/powerbi-learning-6-6-publish-to-web/
b) if your report is contains business data, then you should go for power bi embedded (please note the following is changed due to recent changes to power bi, I have not done migration yet)
-in this case you should develop Power bi report, create workspace collection in Azure and upload your report there
-there is a power bi provision application available, that you can use to operate with power bi embedded Azure service (it's a console app and takes some time to get used to)
-in my case (it's working on the old mode, will try to update here after I will migrate to the new version, if I will do it at all... depends on the new pricing...)
This is a code to create embed token for Power BI report
One more thing regarding Provision application - after you upload you report, you have to update Connection string - you can do it using the same Provision application - there you have to enter your data source credentials
I hope this helps...
using Microsoft.PowerBI.Api.V1;
using Microsoft.PowerBI.Api.V1.Models;
using Microsoft.PowerBI.Security;
using Microsoft.Rest;
using System;
using System.Collections.Generic;
using System.Linq;
namespace PowerBiReportingService.PowerBI
{
public class PowerReports : IDisposable
{
private const string APP_KEY = "sdlfjalkfjalsjdlfa..."; // you get it from Azure portal Power BI workspace collection -> Access keys
private const string BASE_URL = "https://api.powerbi.com"; //this is the same for all
private const string REPORT_COLLECTION = "CollectionName"; //as you named it in Azure portal
private const string WORKSPACE_ID = "8v3a8713-...."; //this is what you get when you upload your report to power bi collection using Power BI report Provision console application - there you have option to get report Id
private string ReportId;
private string ReportUrl;
private IList<Report> Reports { get; set; }
public string AccessToken { get; set; }
public PowerReports()
{
var credentials = new TokenCredentials(APP_KEY, "AppKey");
using (var client = new PowerBIClient(credentials))
{
client.BaseUri = new Uri(BASE_URL);
var reportsResponse = client.Reports.GetReports(REPORT_COLLECTION, WORKSPACE_ID);
Reports = reportsResponse.Value;
var report = Reports.FirstOrDefault();
if (report == null)
throw new InvalidOperationException("Power BI analysis services not configured / or unexpected error!");
ReportId = report.Id;
ReportUrl = report.EmbedUrl;
}
}
public string GetEmbedReportUrl()
{
var embedToken = PowerBIToken.CreateReportEmbedToken(REPORT_COLLECTION, WORKSPACE_ID, ReportId);
AccessToken = embedToken.Generate(APP_KEY);
return ReportUrl;
}
public void Dispose()
{
Reports = null;
AccessToken = string.Empty;
}
}
}