“Power BI has allowed us to automate mundane work, create efficient workflows, and make data-driven decisions.” - Microsoft Reference
public class PowerBIDemo
{
public async Task< PowerBIClient > GetPowerBiClient()
{
var tokenCredentials = new TokenCredentials(ACCESS_TOKEN, "Bearer");
return new PowerBIClient(https://api.powerbi.com/, tokenCredentials);
}
public async Task< Report > GetReport(string workspaceId, string reportId)
{
PowerBIClient client = await GetPowerBiClient();
return await client.Reports.GetReportInGroupAsync(new Guid(workspaceId), new Guid(reportId));
}
public async Task< Dataset > GetDataset(string workspaceId, string datasetId)
{
PowerBIClient client = await GetPowerBiClient();
return await client.Datasets.GetDatasetInGroupAsync(new Guid(workspaceId), datasetId);
}
public async Task< Report > CloneReport(Guid workspaceId, Guid reportId, Guid newWorkspaceId, string datasetId)
{
PowerBIClient client = await GetPowerBiClient();
return await client.Reports.CloneReportInGroupAsync(
workspaceId,
reportId,
new CloneReportRequest
{
Name = "Report_Name",
TargetWorkspaceId = newWorkspaceId,
TargetModelId = datasetId
});
}
public async Task RebindReportToNewDataset(string workspaceId, Report report, string newDatasetId)
{
PowerBIClient client = await GetPowerBiClient();
await client.Reports.RebindReportInGroupAsync(
new Guid(workspaceId),
report.Id,
new RebindReportRequest
{
DatasetId = newDatasetId
});
}
public async Task ImportPbixFile(Stream fileStream, string workspaceId, string templateName)
{
PowerBIClient client = await GetPowerBiClient();
Import import = await client.Imports.PostImportWithFileAsyncInGroup(new Guid(workspaceId), fileStream, templateName);
}
}
// Get models. models contains enums that can be used.
var models = window['powerbi-client'].models;
// We give All permissions to demonstrate switching between View and Edit mode and saving report.
var permissions = models.Permissions.All;
var config = {
type: 'report',
tokenType: tokenType == '0' ? models.TokenType.Aad : models.TokenType.Embed,
accessToken: txtAccessToken,
embedUrl: txtEmbedUrl,
id: txtEmbedReportId,
permissions: permissions,
//viewMode: models.ViewMode.Edit, //by default the embedding type is view mode. If we uncomment this the report will be embedded in edit mode.
settings: {
panes: {
filters: {
visible: true
},
pageNavigation: {
visible: true
}
}
}
};
// Get a reference to the embedded report HTML element
var embedContainer = $('#embedContainer')[0];
// Embed the report and display it within the div container.
var report = powerbi.embed(embedContainer, config);
// Report.off removes a given event handler if it exists.
report.off("loaded");
// Report.on will add an event handler which prints to Log window.
report.on("loaded", function () {
Log.logText("Loaded");
});
// Report.off removes a given event handler if it exists.
report.off("rendered");
// Report.on will add an event handler which prints to Log window.
report.on("rendered", function () {
Log.logText("Rendered");
});
report.on("error", function (event) {
Log.log(event.detail);
report.off("error");
});
report.off("saved");
report.on("saved", function (event) {
Log.log(event.detail);
if (event.detail.saveAs) {
Log.logText('In order to interact with the new report, create a new token and load the new report');
}
});