Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated embedded PBI report generator #261

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
289 changes: 98 additions & 191 deletions samples/PowerBi/EmbedYourPBIApp/PBIEmbedGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ pageextension 50100 BusinessManagerRoleCenterExt extends "Business Manager Role
}
sb.AppendLine("");

sb.AppendLine("namespace " + alNamespace);
sb.AppendLine($"namespace {alNamespace};");
sb.AppendLine("");
sb.AppendLine($"using {rcExt.Attributes["rolecenternamespace"].Value};");
sb.AppendLine("");

sb.AppendLine("pageextension " + rcExt.Attributes["id"].Value + " \"" + rcExt.Attributes["name"].Value + "\" extends \"" + rcExt.Attributes["extends"].Value + "\"");
Expand Down Expand Up @@ -220,7 +222,7 @@ public static string GeneratePBIRCExtensionActionCode(XmlNode action, StringBuil
sb.AppendLine("");

indents(sb, 4);
sb.AppendLine("action(" + action.Attributes["name"].Value + ")");
sb.AppendLine($"action(\"{action.Attributes["name"].Value}\")");

indents(sb, 4);
sb.AppendLine("{");
Expand All @@ -246,210 +248,109 @@ public static string GeneratePBIRCExtensionActionCode(XmlNode action, StringBuil
return sb.ToString();
}


public static string GeneratePBIEmbedPageCode(string alNamespace, XmlNode query, Version version)
{
StringBuilder sb = new StringBuilder();
string context = query.Attributes["id"].Value + "-" + query.Attributes["name"].Value;

/*
// Auto-generated al file for PBI embed page <id>
//
// Adding a PBI embed page for this PBI content:
// PBI ReportId = '<PBIReportId>'
// PBI ReportName = '<PBIReportName>'

namespace Microsoft.PowerBIApps

page <id> "<name>"
{
UsageCategory = ReportsAndAnalysis;
Caption = '<caption>';
AboutTitle = '<aboutTitle>';
AboutText = '<aboutText>';

layout
{
area(Content)
{
part(EmbeddedReport; "Power BI Embedded Report Part")
{
Caption = 'Financial Overview';
SubPageView = where(Context = const('<id>-<name>'));
ApplicationArea = All;
}
}
}
*/

sb.AppendLine("// Auto-generated al file for PBI embed page " + query.Attributes["id"].Value);
sb.AppendLine("// ");
sb.AppendLine("// Adding a PBI embed page for this PBI content:");
sb.AppendLine("// PBI ReportId = '" + query.Attributes["PBIReportId"].Value);
sb.AppendLine("// PBI ReportName = '" + query.Attributes["PBIReportName"].Value);
sb.AppendLine($"// Auto-generated al file for PBI embed page {query.Attributes["id"].Value}");
sb.AppendLine($"// Report: {query.Attributes["PBIReportName"].Value}");
sb.AppendLine($"// Page: {query.Attributes["PBIReportPage"].Value}");
sb.AppendLine("");

sb.AppendLine("namespace " + alNamespace);
sb.AppendLine($"namespace {alNamespace};");
sb.AppendLine("");

sb.AppendLine("page " + query.Attributes["id"].Value + " \"" + query.Attributes["name"].Value + "\"");
sb.AppendLine("using Microsoft.PowerBIReports;");
sb.AppendLine("using System.Integration.PowerBI;");
sb.AppendLine("using System.Environment.Configuration;");
sb.AppendLine("");
sb.AppendLine($"page {query.Attributes["id"].Value} \"{query.Attributes["name"].Value}\"");
sb.AppendLine("{");

indentAppendLine(sb, 1, "UsageCategory = ReportsAndAnalysis;");
indentAppendLine(sb, 1, "Caption = '" + query.Attributes["caption"].Value + "';");
indentAppendLine(sb, 1, "AboutTitle = '" + query.Attributes["aboutTitle"].Value + "'; ");
indentAppendLine(sb, 1, "AboutText = '" + query.Attributes["aboutText"].Value + "'; ");
sb.AppendLine(" UsageCategory = ReportsAndAnalysis;");
sb.AppendLine($" Caption = '{query.Attributes["caption"].Value}';");
sb.AppendLine($" AboutTitle = '{query.Attributes["aboutTitle"].Value}';");
sb.AppendLine($" AboutText = '{query.Attributes["aboutText"].Value}';");
sb.AppendLine("");

indentAppendLine(sb, 1, "layout");
indentAppendLine(sb, 1, "{");

indentAppendLine(sb, 2, "area(Content)");
indentAppendLine(sb, 2, "{");

indentAppendLine(sb, 3, "part(EmbeddedReport; \"Power BI Embedded Report Part\")");
indentAppendLine(sb, 3, "{");

indentAppendLine(sb, 4, "ApplicationArea = All;");
indentAppendLine(sb, 4, "Caption = '" + query.Attributes["caption"].Value + "';");
indentAppendLine(sb, 4, "SubPageView = where(Context = const ('" + context + "'));");

indentAppendLine(sb, 3, "}");

indentAppendLine(sb, 2, "}");

indentAppendLine(sb, 1, "}");
sb.AppendLine(" layout");
sb.AppendLine(" {");
sb.AppendLine(" area(Content)");
sb.AppendLine(" {");
sb.AppendLine(" usercontrol(PowerBIAddin; PowerBIManagement)");
sb.AppendLine(" {");
sb.AppendLine(" ApplicationArea = All;");
sb.AppendLine("");

/*
trigger OnOpenPage()
var
PowerBIDisplayedElement: Record "Power BI Displayed Element";
PowerBIServiceMgt: Codeunit "Power BI Service Mgt.";
begin
EnsureUserAcceptedPowerBITerms();

// Cleanup previously added reports for this context
PowerBIDisplayedElement.SetRange(Context, vContext);
PowerBIDisplayedElement.DeleteAll();
PowerBIDisplayedElement.SetRange(Context);

// Add the report
PowerBIServiceMgt.AddReportForContext(vReportId, vContext);

// Customize page and other settings
PowerBIDisplayedElement.Get(UserSecurityId(), vContext, PowerBIDisplayedElement.MakeReportKey(vReportId),
PowerBIDisplayedElement.ElementType::Report);
PowerBIDisplayedElement.ReportPage := vReportPage;
PowerBIDisplayedElement.ShowPanesInExpandedMode := true;
PowerBIDisplayedElement.ShowPanesInNormalMode := true;
PowerBIDisplayedElement.Modify();

CurrPage.EmbeddedReport.Page.SetFullPageMode(true);
end;
*/
indentAppendLine(sb, 1, "trigger OnOpenPage()");
indentAppendLine(sb, 1, "var");

indentAppendLine(sb, 2, "PowerBIDisplayedElement: Record \"Power BI Displayed Element\";");
indentAppendLine(sb, 2, "PowerBIServiceMgt: Codeunit \"Power BI Service Mgt.\";");

indentAppendLine(sb, 1, "begin");

indentAppendLine(sb, 2, "EnsureUserAcceptedPowerBITerms();");
sb.AppendLine(" trigger ControlAddInReady()");
sb.AppendLine(" begin");
sb.AppendLine(" InitializeAddIn();");
sb.AppendLine(" end;");
sb.AppendLine("");
indentAppendLine(sb, 2, "// Cleanup previously added reports for this context");
indentAppendLine(sb, 2, "PowerBIDisplayedElement.SetRange(Context, vContext);");
indentAppendLine(sb, 2, "PowerBIDisplayedElement.DeleteAll();");
indentAppendLine(sb, 2, "PowerBIDisplayedElement.SetRange(Context);");
sb.AppendLine(" trigger ErrorOccurred(Operation: Text; ErrorText: Text)");
sb.AppendLine(" begin");
sb.AppendLine(" ShowErrorNotification(Operation, ErrorText);");
sb.AppendLine(" end;");
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine("");
indentAppendLine(sb, 2, "// Add the report");
indentAppendLine(sb, 2, "PowerBIServiceMgt.AddReportForContext(vReportId, vContext);");
sb.AppendLine(" actions");
sb.AppendLine(" {");
sb.AppendLine(" area(processing)");
sb.AppendLine(" {");
sb.AppendLine(" action(FullScreen)");
sb.AppendLine(" {");
sb.AppendLine(" ApplicationArea = All;");
sb.AppendLine(" Caption = 'Fullscreen';");
sb.AppendLine(" ToolTip = 'Shows the Power BI element as full screen.';");
sb.AppendLine(" Image = View;");
sb.AppendLine(" Visible = false;");
sb.AppendLine("");
indentAppendLine(sb, 2, "// Customize page and other settings");
indentAppendLine(sb, 2, "PowerBIDisplayedElement.Get(UserSecurityId(), vContext, PowerBIDisplayedElement.MakeReportKey(vReportId), PowerBIDisplayedElement.ElementType::Report);");
indentAppendLine(sb, 2, "PowerBIDisplayedElement.ReportPage := vReportPage;");
indentAppendLine(sb, 2, "PowerBIDisplayedElement.ShowPanesInExpandedMode := true;");
indentAppendLine(sb, 2, "PowerBIDisplayedElement.ShowPanesInNormalMode := true;");
indentAppendLine(sb, 2, "PowerBIDisplayedElement.Modify();");
sb.AppendLine(" trigger OnAction()");
sb.AppendLine(" begin");
sb.AppendLine(" CurrPage.PowerBIAddin.FullScreen();");
sb.AppendLine(" end;");
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine(" }");
sb.AppendLine("");

if (version == new Version() | version >= new Version("24.2"))
{
indentAppendLine(sb, 2, "CurrPage.EmbeddedReport.Page.SetFullPageMode(true);");
}

indentAppendLine(sb, 1, "end;");
sb.AppendLine(" var");
sb.AppendLine(" ReportId: Guid;");
sb.AppendLine(" ReportPage: Text;");
sb.AppendLine(" ErrorNotificationMsg: Label 'An error occurred while loading Power BI. Your Power BI embedded content might not work. Here are the error details: \"%1: %2\"', Comment = '%1: a short error code. %2: a verbose error message in english';");
sb.AppendLine(" PowerBIEmbedReportUrlTemplateTxt: Label 'https://app.powerbi.com/reportEmbed?reportId=%1', Locked = true;");
sb.AppendLine("");

/*
local procedure EnsureUserAcceptedPowerBITerms()
var
PowerBIContextSettings: Record "Power BI Context Settings";
PowerBIEmbedSetupWizard: Page "Power BI Embed Setup Wizard";
begin
PowerBIContextSettings.SetRange(UserSID, UserSecurityId());
if PowerBIContextSettings.IsEmpty() then begin
PowerBIEmbedSetupWizard.SetContext(vContext);
if PowerBIEmbedSetupWizard.RunModal() <> Action::OK then;

if PowerBIContextSettings.IsEmpty() then
Error(PowerBiNotSetupErr);
end;

PowerBIContextSettings.CreateOrReadForCurrentUser(vContext);
if not PowerBIContextSettings.LockToSelectedElement then begin
PowerBIContextSettings.LockToSelectedElement := true;
PowerBIContextSettings.Modify();
end;
end;
*/
indentAppendLine(sb, 1, "local procedure EnsureUserAcceptedPowerBITerms()");
indentAppendLine(sb, 1, "var");

indentAppendLine(sb, 2, "PowerBIContextSettings: Record \"Power BI Context Settings\";");
indentAppendLine(sb, 2, "PowerBIEmbedSetupWizard: Page \"Power BI Embed Setup Wizard\";");

indentAppendLine(sb, 1, "begin");

indentAppendLine(sb, 2, "PowerBIContextSettings.SetRange(UserSID, UserSecurityId());");
indentAppendLine(sb, 2, "if PowerBIContextSettings.IsEmpty() then begin");

indentAppendLine(sb, 3, "PowerBIEmbedSetupWizard.SetContext(vContext);");
indentAppendLine(sb, 3, "if PowerBIEmbedSetupWizard.RunModal() <> Action::OK then;");
sb.AppendLine(" trigger OnOpenPage()");
sb.AppendLine(" var");
sb.AppendLine(" PowerBIReportsSetup: Record \"PowerBI Reports Setup\";");
sb.AppendLine(" begin");
sb.AppendLine(" PowerBIReportsSetup.EnsureUserAcceptedPowerBITerms();");
sb.AppendLine("");
indentAppendLine(sb, 3, "if PowerBIContextSettings.IsEmpty() then");

indentAppendLine(sb, 4, "Error(PowerBiNotSetupErr);");

indentAppendLine(sb, 2, "end;");
sb.AppendLine(" // Replace with your own report id and page");
sb.AppendLine($" PowerBIReportsSetup.GetReportIdAndEnsureSetup(PowerBIReportsSetup.FieldNo(\"{query.Attributes["PBIReportIdFieldName"].Value}\"), Enum::\"Embedded Report Type\"::\"{query.Attributes["PBIReportPage"].Value}\", ReportId, ReportPage);");
sb.AppendLine(" end;");
sb.AppendLine("");
indentAppendLine(sb, 2, "PowerBIContextSettings.CreateOrReadForCurrentUser(vContext);");
indentAppendLine(sb, 2, "if not PowerBIContextSettings.LockToSelectedElement then begin");

indentAppendLine(sb, 3, "PowerBIContextSettings.LockToSelectedElement := true;");
indentAppendLine(sb, 3, "PowerBIContextSettings.Modify();");

indentAppendLine(sb, 2, "end;");

indentAppendLine(sb, 1, "end;");
sb.AppendLine(" local procedure ShowErrorNotification(ErrorCategory: Text; ErrorMessage: Text)");
sb.AppendLine(" var");
sb.AppendLine(" PowerBIContextSettings: Record \"Power BI Context Settings\";");
sb.AppendLine(" NotificationLifecycleMgt: Codeunit \"Notification Lifecycle Mgt.\";");
sb.AppendLine(" Notify: Notification;");
sb.AppendLine(" begin");
sb.AppendLine(" Notify.Id := CreateGuid();");
sb.AppendLine(" Notify.Message(StrSubstNo(ErrorNotificationMsg, ErrorCategory, ErrorMessage));");
sb.AppendLine(" Notify.Scope := NotificationScope::LocalScope;");
sb.AppendLine("");

/*
var
PowerBiNotSetupErr: Label 'Power BI is not set up. You need to set up Power BI in order to see this report.';
vReportId: Label '<PBIReportId>', Locked = true;
vReportPage: Label '<PBIReportPage>', Locked = true;
vContext: Label '<id>-<name>', MaxLength = 30, Locked = true, Comment = 'IMPORTANT: keep it unique across pages. Also, make sure this value is the same used in the SubPageView above.';
*/
indentAppendLine(sb, 1, "var");

indentAppendLine(sb, 2, "PowerBiNotSetupErr: Label 'Power BI is not set up. You need to set up Power BI in order to see this report.';");
indentAppendLine(sb, 2, "vReportId: Label '" + query.Attributes["PBIReportId"].Value + "', Locked = true;");
indentAppendLine(sb, 2, "vReportPage: Label '" + query.Attributes["PBIReportPage"].Value + "', Locked = true;");
indentAppendLine(sb, 2, "vContext: Label '" + context + "', MaxLength = 30, Locked = true, Comment = 'IMPORTANT: keep it unique across pages. Also, make sure this value is the same used in the SubPageView above.';");

sb.AppendLine(" NotificationLifecycleMgt.SendNotification(Notify, PowerBIContextSettings.RecordId());");
sb.AppendLine(" end;");
sb.AppendLine("");
sb.AppendLine(" local procedure InitializeAddIn()");
sb.AppendLine(" var");
sb.AppendLine(" PowerBIServiceMgt: Codeunit \"Power BI Service Mgt.\";");
sb.AppendLine(" begin");
sb.AppendLine(" PowerBiServiceMgt.InitializeAddinToken(CurrPage.PowerBIAddin);");
sb.AppendLine(" CurrPage.PowerBIAddin.SetSettings(false, true, false, false, false, false, true);");
sb.AppendLine("");
sb.AppendLine(" CurrPage.PowerBIAddin.EmbedPowerBIReport(");
sb.AppendLine(" StrSubstNo(PowerBIEmbedReportUrlTemplateTxt, ReportId),");
sb.AppendLine(" ReportId,");
sb.AppendLine(" ReportPage);");
sb.AppendLine(" end;");
sb.AppendLine("}");

return sb.ToString();
Expand Down Expand Up @@ -480,7 +381,7 @@ permissionset 50120 "Power BI Finance App - Objects"

indentAppendLine(sb, 0, "// Auto-generated al file for PBI permissions " + permSet.Attributes["id"].Value);
indentAppendLine(sb, 0, "");
indentAppendLine(sb, 0, "namespace " + alNamespace);
indentAppendLine(sb, 0, $"namespace {alNamespace};");
indentAppendLine(sb, 0, "");
indentAppendLine(sb, 0, "permissionset " + permSet.Attributes["id"].Value + " \"" + permSet.Attributes["name"].Value + "\"");
indentAppendLine(sb, 0, "{");
Expand All @@ -490,8 +391,14 @@ permissionset 50120 "Power BI Finance App - Objects"

foreach (XmlNode page in pages.SelectNodes("page"))
{
indentAppendLine(sb, 2, "page \"" + page.Attributes["name"].Value + "\" = X,");
}
if (page.Attributes["name"].Value == pages.LastChild.Attributes["name"].Value)
{
indentAppendLine(sb, 2, "page \"" + page.Attributes["name"].Value + "\" = X;");
} else
{
indentAppendLine(sb, 2, "page \"" + page.Attributes["name"].Value + "\" = X,");
}
}

indentAppendLine(sb, 0, "}");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class PBIPageDefinition
string caption { get; set; }
string aboutTitle { get; set; }
string aboutText { get; set; }
string PBIReportId { get; set; }
string PBIReportIdFieldName { get; set; }
string PBIReportName { get; set; }
string PBIReportPage { get; set; }

Expand All @@ -33,7 +33,7 @@ public PBIPageDefinition(
this.caption = caption;
this.aboutTitle = aboutTitle;
this.aboutText = aboutText;
this.PBIReportId = PBIReportId;
this.PBIReportIdFieldName = PBIReportId;
this.PBIReportName = PBIReportName;
this.PBIReportPage = PBIReportPage;
}
Expand All @@ -48,7 +48,7 @@ public XElement asXElement()
new XAttribute("caption", caption),
new XAttribute("aboutTitle", aboutTitle),
new XAttribute("aboutText", aboutText),
new XAttribute("PBIReportId", PBIReportId),
new XAttribute("PBIReportIdFieldName", PBIReportIdFieldName),
new XAttribute("PBIReportName", PBIReportName),
new XAttribute("PBIReportPage", PBIReportPage)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ static private void ExcelRowstoRCExtensionDefinitions(List<string[]> RCExtension
row[2],
row[3],
row[4],
row[5]
row[5],
row[6]
);

if (!row[0].Equals("id"))
Expand Down
Loading