-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathEmbedService.cs
More file actions
353 lines (285 loc) · 14.5 KB
/
EmbedService.cs
File metadata and controls
353 lines (285 loc) · 14.5 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// ----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
// ----------------------------------------------------------------------------
namespace AppOwnsData.Services
{
using AppOwnsData.Models;
using Microsoft.PowerBI.Api;
using Microsoft.PowerBI.Api.Models;
using Microsoft.Rest;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
public static class EmbedService
{
private static readonly string powerBiApiUrl = ConfigurationManager.AppSettings["powerBiApiUrl"];
public static async Task<PowerBIClient> GetPowerBiClient()
{
var tokenCredentials = new TokenCredentials(await AadService.GetAccessToken(), "Bearer");
return new PowerBIClient(new Uri(powerBiApiUrl), tokenCredentials);
}
/// <summary>
/// Get embed params for a report
/// </summary>
/// <returns>Wrapper object containing Embed token, Embed URL, Report Id, and Report name for single report</returns>
public static async Task<ReportEmbedConfig> GetEmbedParams(Guid workspaceId, Guid reportId, [Optional] Guid additionalDatasetId)
{
using (var pbiClient = await GetPowerBiClient())
{
// Get report info
var pbiReport = pbiClient.Reports.GetReportInGroup(workspaceId, reportId);
/*
Check if dataset is present for the corresponding report
If no dataset is present then it is a RDL report
*/
bool isRDLReport = String.IsNullOrEmpty(pbiReport.DatasetId);
EmbedToken embedToken;
if (isRDLReport)
{
// Get Embed token for RDL Report
embedToken = await GetEmbedTokenForRDLReport(workspaceId, reportId);
}
else
{
// Create list of dataset
var datasetIds = new List<Guid>();
// Add dataset associated to the report
datasetIds.Add(Guid.Parse(pbiReport.DatasetId));
// Append additional dataset to the list to achieve dynamic binding later
if (additionalDatasetId != Guid.Empty)
{
datasetIds.Add(additionalDatasetId);
}
// Get Embed token multiple resources
embedToken = await GetEmbedToken(reportId, datasetIds, workspaceId);
}
// Add report data for embedding
var embedReports = new List<EmbedReport>() {
new EmbedReport
{
ReportId = pbiReport.Id, ReportName = pbiReport.Name, EmbedUrl = pbiReport.EmbedUrl
}
};
// Capture embed params
var embedParams = new ReportEmbedConfig
{
EmbedReports = embedReports,
EmbedToken = embedToken
};
return embedParams;
}
}
/// <summary>
/// Get embed params for multiple reports for a single workspace
/// </summary>
/// <returns>Wrapper object containing Embed token, Embed URL, Report Id, and Report name for multiple reports</returns>
/// <remarks>This function is not supported for RDL Report</remakrs>
public static async Task<ReportEmbedConfig> GetEmbedParams(Guid workspaceId, IList<Guid> reportIds, [Optional] IList<Guid> additionalDatasetIds)
{
// Note: This method is an example and is not consumed in this sample app
using (var pbiClient = await GetPowerBiClient())
{
// Create mapping for reports and Embed URLs
var embedReports = new List<EmbedReport>();
// Create list of datasets
var datasetIds = new List<Guid>();
// Get datasets and Embed URLs for all the reports
foreach (var reportId in reportIds)
{
// Get report info
var pbiReport = pbiClient.Reports.GetReportInGroup(workspaceId, reportId);
// Append to existing list of datasets to achieve dynamic binding later
datasetIds.Add(Guid.Parse(pbiReport.DatasetId));
// Add report data for embedding
embedReports.Add(new EmbedReport { ReportId = pbiReport.Id, ReportName = pbiReport.Name, EmbedUrl = pbiReport.EmbedUrl });
}
// Append to existing list of datasets to achieve dynamic binding later
if (additionalDatasetIds != null)
{
datasetIds.AddRange(additionalDatasetIds);
}
// Get Embed token multiple resources
var embedToken = await GetEmbedToken(reportIds, datasetIds, workspaceId);
// Capture embed params
var embedParams = new ReportEmbedConfig
{
EmbedReports = embedReports,
EmbedToken = embedToken
};
return embedParams;
}
}
/// <summary>
/// Get Embed token for single report, multiple datasets, and an optional target workspace
/// </summary>
/// <returns>Embed token</returns>
/// <remarks>This function is not supported for RDL Report</remakrs>
public static async Task<EmbedToken> GetEmbedToken(Guid reportId, IList<Guid> datasetIds, [Optional] Guid targetWorkspaceId)
{
using (var pbiClient = await GetPowerBiClient())
{
// Create a request for getting Embed token
// This method works only with new Power BI V2 workspace experience
var tokenRequest = new GenerateTokenRequestV2(
reports: new List<GenerateTokenRequestV2Report>() { new GenerateTokenRequestV2Report(reportId) },
datasets: datasetIds.Select(datasetId => new GenerateTokenRequestV2Dataset(datasetId.ToString())).ToList(),
targetWorkspaces: targetWorkspaceId != Guid.Empty ? new List<GenerateTokenRequestV2TargetWorkspace>() { new GenerateTokenRequestV2TargetWorkspace(targetWorkspaceId) } : null
);
// Generate Embed token
var embedToken = pbiClient.EmbedToken.GenerateToken(tokenRequest);
return embedToken;
}
}
/// <summary>
/// Get Embed token for multiple reports, datasets, and an optional target workspace
/// </summary>
/// <returns>Embed token</returns>
/// <remarks>This function is not supported for RDL Report</remakrs>
public static async Task<EmbedToken> GetEmbedToken(IList<Guid> reportIds, IList<Guid> datasetIds, [Optional] Guid targetWorkspaceId)
{
// Note: This method is an example and is not consumed in this sample app
using (var pbiClient = await GetPowerBiClient())
{
// Convert reports to required types
var reports = reportIds.Select(reportId => new GenerateTokenRequestV2Report(reportId)).ToList();
// Convert datasets to required types
var datasets = datasetIds.Select(datasetId => new GenerateTokenRequestV2Dataset(datasetId.ToString())).ToList();
// Create a request for getting Embed token
// This method works only with new Power BI V2 workspace experience
var tokenRequest = new GenerateTokenRequestV2(
datasets: datasets,
reports: reports,
targetWorkspaces: targetWorkspaceId != Guid.Empty ? new List<GenerateTokenRequestV2TargetWorkspace>() { new GenerateTokenRequestV2TargetWorkspace(targetWorkspaceId) } : null
);
// Generate Embed token
var embedToken = pbiClient.EmbedToken.GenerateToken(tokenRequest);
return embedToken;
}
}
/// <summary>
/// Get Embed token for multiple reports, datasets, and optional target workspaces
/// </summary>
/// <returns>Embed token</returns>
/// <remarks>This function is not supported for RDL Report</remakrs>
public static async Task<EmbedToken> GetEmbedToken(IList<Guid> reportIds, IList<Guid> datasetIds, [Optional] IList<Guid> targetWorkspaceIds)
{
// Note: This method is an example and is not consumed in this sample app
using (var pbiClient = await GetPowerBiClient())
{
// Convert report Ids to required types
var reports = reportIds.Select(reportId => new GenerateTokenRequestV2Report(reportId)).ToList();
// Convert dataset Ids to required types
var datasets = datasetIds.Select(datasetId => new GenerateTokenRequestV2Dataset(datasetId.ToString())).ToList();
// Convert target workspace Ids to required types
IList<GenerateTokenRequestV2TargetWorkspace> targetWorkspaces = null;
if (targetWorkspaceIds != null)
{
targetWorkspaces = targetWorkspaceIds.Select(targetWorkspaceId => new GenerateTokenRequestV2TargetWorkspace(targetWorkspaceId)).ToList();
}
// Create a request for getting Embed token
// This method works only with new Power BI V2 workspace experience
var tokenRequest = new GenerateTokenRequestV2(
datasets: datasets,
reports: reports,
targetWorkspaces: targetWorkspaceIds != null ? targetWorkspaces : null
);
// Generate Embed token
var embedToken = pbiClient.EmbedToken.GenerateToken(tokenRequest);
return embedToken;
}
}
/// <summary>
/// Get Embed token for RDL Report
/// </summary>
/// <returns>Embed token</returns>
public static async Task<EmbedToken> GetEmbedTokenForRDLReport(Guid targetWorkspaceId, Guid reportId, string accessLevel = "view")
{
using (var pbiClient = await GetPowerBiClient())
{
// Generate token request for RDL Report
var generateTokenRequestParameters = new GenerateTokenRequest(
accessLevel: accessLevel
);
// Generate Embed token
var embedToken = pbiClient.Reports.GenerateTokenInGroup(targetWorkspaceId, reportId, generateTokenRequestParameters);
return embedToken;
}
}
/// <summary>
/// Get embed params for a dashboard
/// </summary>
/// <returns>Wrapper object containing Embed token, Embed URL for single dashboard</returns>
public static async Task<DashboardEmbedConfig> EmbedDashboard(Guid workspaceId)
{
// Create a Power BI Client object. It will be used to call Power BI APIs.
using (var client = await GetPowerBiClient())
{
// Get a list of dashboards.
var dashboards = await client.Dashboards.GetDashboardsInGroupAsync(workspaceId);
// Get the first report in the workspace.
var dashboard = dashboards.Value.FirstOrDefault();
if (dashboard == null)
{
throw new NullReferenceException("Workspace has no dashboards");
}
// Generate Embed Token.
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
var tokenResponse = await client.Dashboards.GenerateTokenInGroupAsync(workspaceId, dashboard.Id, generateTokenRequestParameters);
if (tokenResponse == null)
{
throw new NullReferenceException("Failed to generate embed token");
}
// Generate Embed Configuration.
var dashboardEmbedConfig = new DashboardEmbedConfig
{
EmbedToken = tokenResponse,
EmbedUrl = dashboard.EmbedUrl,
DashboardId = dashboard.Id
};
return dashboardEmbedConfig;
}
}
/// <summary>
/// Get embed params for a tile
/// </summary>
/// <returns>Wrapper object containing Embed token, Embed URL for single tile</returns>
public static async Task<TileEmbedConfig> EmbedTile(Guid workspaceId)
{
// Create a Power BI Client object. It will be used to call Power BI APIs.
using (var client = await GetPowerBiClient())
{
// Get a list of dashboards.
var dashboards = await client.Dashboards.GetDashboardsInGroupAsync(workspaceId);
// Get the first report in the workspace.
var dashboard = dashboards.Value.FirstOrDefault();
if (dashboard == null)
{
throw new NullReferenceException("Workspace has no dashboards");
}
var tiles = await client.Dashboards.GetTilesInGroupAsync(workspaceId, dashboard.Id);
// Get the first tile in the workspace.
var tile = tiles.Value.FirstOrDefault();
// Generate Embed Token for a tile.
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
var tokenResponse = await client.Tiles.GenerateTokenInGroupAsync(workspaceId, dashboard.Id, tile.Id, generateTokenRequestParameters);
if (tokenResponse == null)
{
throw new NullReferenceException("Failed to generate embed token");
}
// Generate Embed Configuration.
var tileEmbedConfig = new TileEmbedConfig()
{
EmbedToken = tokenResponse,
EmbedUrl = tile.EmbedUrl,
TileId = tile.Id,
DashboardId = dashboard.Id
};
return tileEmbedConfig;
}
}
}
}