-
Notifications
You must be signed in to change notification settings - Fork 1
/
Default.aspx.cs
88 lines (74 loc) · 3.17 KB
/
Default.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.PowerBI;
using Microsoft.PowerBI.AspNet.WebForms;
using Microsoft.PowerBI.Security;
using System.Configuration;
using Microsoft.PowerBI.Api.Beta;
using Microsoft.Rest;
using PBIEmbeddedWebForms.Models;
namespace PBIEmbeddedWebForms
{
public partial class _Default : Page
{
private static string workspaceCollection = ConfigurationManager.AppSettings["powerbi:WorkspaceCollection"];
private static string workspaceId = ConfigurationManager.AppSettings["powerbi:WorkspaceId"];
private static string accessKey = ConfigurationManager.AppSettings["powerbi:AccessKey"];
private static string apiUrl = ConfigurationManager.AppSettings["powerbi:ApiUrl"];
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<ReportViewModel> reportsList = new List<ReportViewModel>();
var devToken = PowerBIToken.CreateDevToken(workspaceCollection, workspaceId);
using (var client = this.CreatePowerBIClient(devToken))
{
var reportsResponse = client.Reports.GetReports(workspaceCollection, workspaceId);
for (int i = 0; i < reportsResponse.Value.ToList().Count; i++)
{
reportsList.Add(new ReportViewModel
{
Id = reportsResponse.Value[i].Id,
Name = reportsResponse.Value[i].Id,
EmbedUrl = reportsResponse.Value[i].EmbedUrl,
WebUrl = reportsResponse.Value[i].WebUrl,
Report = reportsResponse.Value[i]
}
);
}
}
var reportId = reportsList[0].Id;
using (var client = this.CreatePowerBIClient(devToken))
{
var embedToken = PowerBIToken.CreateReportEmbedToken(workspaceCollection, workspaceId, reportId);
var viewModel = new ReportViewModel
{
Report = reportsList[0].Report,
AccessToken = embedToken.Generate(accessKey)
};
accessTokenText.Value = viewModel.AccessToken;
embedUrlText.Value = viewModel.Report.EmbedUrl;
}
}
}
/// <summary>
/// This is a helper method that create a jason web token from dev token
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
private IPowerBIClient CreatePowerBIClient(PowerBIToken token)
{
var jasonWebToken = token.Generate(accessKey);
var credentials = new TokenCredentials(jasonWebToken, "AppToken");
var client = new PowerBIClient(credentials)
{
BaseUri = new Uri(apiUrl)
};
return client;
}
}
}