Skip to content

Commit db6f64d

Browse files
authored
Merge pull request microsoft#53 from Microsoft/mahartov/saasSampleUpdates
Changed Saas report sample to use PBI API and added report ID to config file
2 parents 6154208 + 6df7fba commit db6f64d

10 files changed

Lines changed: 201 additions & 85 deletions

File tree

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1-
<?xml version="1.0"?>
1+
<?xml version="1.0"?>
22
<PBIWebApp.Properties.Settings>
3-
<setting name="ClientID" serializeAs="String">
4-
<value>{Enter your app ClientID}</value>
5-
</setting>
6-
<setting name="ClientSecret" serializeAs="String">
7-
<value>{Enter your app SecretKey}</value>
8-
</setting>
9-
<setting name="AADAuthorityUri" serializeAs="String">
10-
<value>https://login.windows.net/common/oauth2/authorize/</value>
11-
</setting>
12-
<setting name="PowerBiAPI" serializeAs="String">
13-
<value>https://analysis.windows.net/powerbi/api</value>
14-
</setting>
15-
<setting name="PowerBiDataset" serializeAs="String">
16-
<value>https://api.powerbi.com/v1.0/myorg/</value>
17-
</setting>
18-
<setting name="RedirectUrl" serializeAs="String">
19-
<value>http://localhost:13526/</value>
20-
</setting>
3+
<setting name="ClientID" serializeAs="String">
4+
<value>{Enter your app ClientID}</value>
5+
</setting>
6+
<setting name="ClientSecret" serializeAs="String">
7+
<value>{Enter your app SecretKey}</value>
8+
</setting>
9+
<setting name="PowerBiAPIResource" serializeAs="String">
10+
<value>https://analysis.windows.net/powerbi/api</value>
11+
</setting>
12+
<setting name="AADAuthorityUri" serializeAs="String">
13+
<value>https://login.windows.net/common/oauth2/authorize/</value>
14+
</setting>
15+
<setting name="PowerBiDataset" serializeAs="String">
16+
<value>https://api.powerbi.com/v1.0/myorg/</value>
17+
</setting>
18+
<setting name="RedirectUrl" serializeAs="String">
19+
<value>http://localhost:13526/</value>
20+
</setting>
21+
<setting name="PowerBiApiUrl" serializeAs="String">
22+
<value>https://api.powerbi.com/</value>
23+
</setting>
24+
<setting name="GroupId" serializeAs="String">
25+
<value></value>
26+
</setting>
27+
<setting name="ReportId" serializeAs="String">
28+
<value></value>
29+
</setting>
2130
</PBIWebApp.Properties.Settings>

User Owns Data/integrate-report-web-app/PBIWebApp/Default.aspx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// This object is used when calling powerbi.embed.
2525
// This also includes settings and options such as filters.
2626
// You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
27-
var config= {
27+
var config = {
2828
type: 'report',
2929
accessToken: accessToken,
3030
embedUrl: embedUrl,
@@ -68,20 +68,22 @@
6868
Power BI Embed Report
6969
</h1>
7070
<h2>
71-
Basic Sample <br />
71+
Basic Sample
72+
<br />
7273
First make sure you <a href="https://dev.powerbi.com/apps">register your app</a>. After registration, copy <u>Client ID</u> and <u>Client Secret</u> to web.config file.
74+
<br />
75+
The application will embed the first report from your Power BI account. If you wish to embed a specific report, please copy the report's Report ID and corresponding Group ID to web.config file.
7376
</h2>
7477
</header>
75-
78+
7679
<div>
77-
<h3>
78-
Select <b>"Get Report"</b> to get and embed first report from your Power BI account.
80+
<h3>Select <b>"Get Report"</b> to embed the report.
7981
</h3>
80-
<asp:Button ID="getReportButton" runat="server" OnClick="getReportButton_Click" Text="Get Report" />
82+
<asp:Button ID="getReportButton" runat="server" OnClick="getReportButton_Click" Text="Get Report" />
8183
</div>
8284

8385
<div class="field">
84-
<div class="fieldtxt">Report Name</div>
86+
<div class="fieldtxt">Report Name</div>
8587
<asp:Textbox ID="txtReportName" runat="server" Width="750px"></asp:Textbox>
8688
</div>
8789

@@ -94,7 +96,9 @@
9496
<div class="fieldtxt">Report Embed URL</div>
9597
<asp:Textbox ID="txtEmbedUrl" runat="server" Width="750px"></asp:Textbox>
9698
</div>
97-
99+
<div class="error">
100+
<asp:Label ID="errorLabel" runat="server"></asp:Label>
101+
</div>
98102
<div>
99103
Embedded Report
100104
<br />
@@ -106,5 +110,4 @@
106110
<br />
107111
<div ID="logView" style="width: 880px;"></div>
108112
</div>
109-
110113
</asp:Content>

User Owns Data/integrate-report-web-app/PBIWebApp/Default.aspx.cs

Lines changed: 91 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-

2-
using System;
1+
using System;
2+
using System.Linq;
33
using System.Web;
44
using System.Web.UI;
55
using System.Collections.Specialized;
6-
using Newtonsoft.Json;
76
using PBIWebApp.Properties;
87
using Microsoft.IdentityModel.Clients.ActiveDirectory;
8+
using Microsoft.PowerBI.Api.V2;
9+
using Microsoft.PowerBI.Api.V2.Models;
10+
using Microsoft.Rest;
911

1012
namespace PBIWebApp
1113
{
@@ -17,7 +19,7 @@ namespace PBIWebApp
1719
*/
1820
public partial class _Default : Page
1921
{
20-
string baseUri = Properties.Settings.Default.PowerBiDataset;
22+
string baseUri = Settings.Default.PowerBiDataset;
2123

2224
protected void Page_Load(object sender, EventArgs e)
2325
{
@@ -51,8 +53,9 @@ protected void Page_Load(object sender, EventArgs e)
5153
//In this sample, you get the first Report. In a production app, you would create a more robost
5254
//solution
5355

54-
//Get first report.
55-
GetReport(0);
56+
//Gets the corresponding report to the setting's ReportId and GroupId.
57+
//If ReportId or GroupId are empty, it will get the first user's report.
58+
GetReport();
5659
}
5760
}
5861

@@ -64,37 +67,42 @@ protected void getReportButton_Click(object sender, EventArgs e)
6467
}
6568

6669

67-
//Get a Report. In this sample, you get the first Report.
68-
protected void GetReport(int index)
70+
// Gets a report based on the setting's ReportId and GroupId.
71+
// If reportId or groupId are empty, it will get the first user's report.
72+
protected void GetReport()
6973
{
70-
//Configure Reports request
71-
System.Net.WebRequest request = System.Net.WebRequest.Create(
72-
String.Format("{0}/Reports",
73-
baseUri)) as System.Net.HttpWebRequest;
74+
var groupId = Settings.Default.GroupId;
75+
var reportId = Settings.Default.ReportId;
76+
var powerBiApiUrl = Settings.Default.PowerBiApiUrl;
7477

75-
request.Method = "GET";
76-
request.ContentLength = 0;
77-
request.Headers.Add("Authorization", String.Format("Bearer {0}", accessToken.Value));
78-
79-
//Get Reports response from request.GetResponse()
80-
using (var response = request.GetResponse() as System.Net.HttpWebResponse)
78+
using (var client = new PowerBIClient(new Uri(powerBiApiUrl), new TokenCredentials(accessToken.Value, "Bearer")))
8179
{
82-
//Get reader from response stream
83-
using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
80+
Report report;
81+
82+
// Settings' group ID is not empty
83+
if (!string.IsNullOrEmpty(groupId))
84+
{
85+
// Gets a report from the group.
86+
report = GetReportFromGroup(client, groupId, reportId);
87+
}
88+
// Settings' report and group Ids are empty, retrieves the user's first report.
89+
else if (string.IsNullOrEmpty(reportId))
90+
{
91+
report = client.Reports.GetReports().Value.FirstOrDefault();
92+
AppendErrorIfReportNull(report, "No reports found. Please specify the target report ID and group in the applications settings.");
93+
}
94+
// Settings contains report ID. (no group ID)
95+
else
96+
{
97+
report = client.Reports.GetReports().Value.FirstOrDefault(r => r.Id == reportId);
98+
AppendErrorIfReportNull(report, string.Format("Report with ID: {0} not found. Please check the report ID. For reports within a group with a group ID, add the group ID to the application's settings", reportId));
99+
}
100+
101+
if (report != null)
84102
{
85-
//Deserialize JSON string
86-
PBIReports Reports = JsonConvert.DeserializeObject<PBIReports>(reader.ReadToEnd());
87-
88-
//Sample assumes at least one Report.
89-
//You could write an app that lists all Reports
90-
if (Reports.value.Length > 0)
91-
{
92-
var report = Reports.value[index];
93-
94-
txtEmbedUrl.Text = report.embedUrl;
95-
txtReportId.Text = report.id;
96-
txtReportName.Text = report.name;
97-
}
103+
txtEmbedUrl.Text = report.EmbedUrl;
104+
txtReportId.Text = report.Id;
105+
txtReportName.Text = report.Name;
98106
}
99107
}
100108
}
@@ -115,7 +123,7 @@ public void GetAuthorizationCode()
115123

116124
//Resource uri to the Power BI resource to be authorized
117125
//The resource uri is hard-coded for sample purposes
118-
{"resource", Properties.Settings.Default.PowerBiAPI},
126+
{"resource", Settings.Default.PowerBiAPIResource},
119127

120128
//After app authenticates, Azure AD will redirect back to the web app. In this sample, Azure AD redirects back
121129
//to Default page (Default.aspx).
@@ -135,7 +143,7 @@ public void GetAuthorizationCode()
135143
// redirect_uri which is the uri that Azure AD will redirect back to after it authenticates
136144

137145
//Redirect to Azure AD to get an authorization code
138-
Response.Redirect(String.Format(Properties.Settings.Default.AADAuthorityUri + "?{0}", queryString));
146+
Response.Redirect(String.Format(Settings.Default.AADAuthorityUri + "?{0}", queryString));
139147
}
140148

141149
public string GetAccessToken(string authorizationCode, string clientID, string clientSecret, string redirectUri)
@@ -148,7 +156,7 @@ public string GetAccessToken(string authorizationCode, string clientID, string c
148156
TokenCache TC = new TokenCache();
149157

150158
//Values are hard-coded for sample purposes
151-
string authority = Properties.Settings.Default.AADAuthorityUri;
159+
string authority = Settings.Default.AADAuthorityUri;
152160
AuthenticationContext AC = new AuthenticationContext(authority, TC);
153161
ClientCredential cc = new ClientCredential(clientID, clientSecret);
154162

@@ -157,18 +165,53 @@ public string GetAccessToken(string authorizationCode, string clientID, string c
157165
authorizationCode,
158166
new Uri(redirectUri), cc).AccessToken;
159167
}
160-
}
161168

162-
//Power BI Reports used to deserialize the Get Reports response.
163-
public class PBIReports
164-
{
165-
public PBIReport[] value { get; set; }
166-
}
167-
public class PBIReport
168-
{
169-
public string id { get; set; }
170-
public string name { get; set; }
171-
public string webUrl { get; set; }
172-
public string embedUrl { get; set; }
169+
// Gets the report with the specified ID from the group. If report ID is emty it will retrieve the first report from the group.
170+
private Report GetReportFromGroup(PowerBIClient client, string groupId, string reportId)
171+
{
172+
// Gets the group by groupId.
173+
var groups = client.Groups.GetGroups();
174+
var sourceGroup = groups.Value.FirstOrDefault(g => g.Id == groupId);
175+
176+
// No group with the group ID was found.
177+
if (sourceGroup == null)
178+
{
179+
errorLabel.Text = string.Format("Group with id: {0} not found. Please validate the provided group ID.", groupId);
180+
return null;
181+
}
182+
183+
Report report = null;
184+
if (string.IsNullOrEmpty(reportId))
185+
{
186+
// Get the first report in the group.
187+
report = client.Reports.GetReportsInGroup(sourceGroup.Id).Value.FirstOrDefault();
188+
AppendErrorIfReportNull(report, "Group doesn't contain any reports.");
189+
}
190+
191+
else
192+
{
193+
try
194+
{
195+
// retrieve a report by the group ID and report ID.
196+
report = client.Reports.GetReportInGroup(groupId, reportId);
197+
}
198+
199+
catch(HttpOperationException)
200+
{
201+
errorLabel.Text = string.Format("Report with ID:{0} not found in the group {1}, Please check the report ID.", reportId, groupId);
202+
203+
}
204+
}
205+
206+
return report;
207+
}
208+
209+
private void AppendErrorIfReportNull(Report report, string errorMessage)
210+
{
211+
if (report == null)
212+
{
213+
errorLabel.Text = errorMessage;
214+
}
215+
}
173216
}
174217
}

User Owns Data/integrate-report-web-app/PBIWebApp/Default.aspx.designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

User Owns Data/integrate-report-web-app/PBIWebApp/PBIWebApp.csproj

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,26 @@
4747
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms">
4848
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.16.204221202\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll</HintPath>
4949
</Reference>
50+
<Reference Include="Microsoft.PowerBI.Api, Version=2.0.12.18114, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
51+
<HintPath>..\packages\Microsoft.PowerBI.Api.2.0.12\lib\portable-net45+win+wpa81+MonoAndroid10+MonoTouch10+xamarinios10+xamarinmac20\Microsoft.PowerBI.Api.dll</HintPath>
52+
<Private>True</Private>
53+
</Reference>
54+
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
55+
<HintPath>..\packages\Microsoft.Rest.ClientRuntime.2.0.1\lib\net45\Microsoft.Rest.ClientRuntime.dll</HintPath>
56+
<Private>True</Private>
57+
</Reference>
58+
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
59+
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
60+
<Private>True</Private>
61+
</Reference>
5062
<Reference Include="System.ComponentModel.DataAnnotations" />
5163
<Reference Include="System" />
5264
<Reference Include="System.Data" />
5365
<Reference Include="System.Core" />
5466
<Reference Include="System.Data.DataSetExtensions" />
67+
<Reference Include="System.Net" />
5568
<Reference Include="System.Net.Http" />
69+
<Reference Include="System.Net.Http.WebRequest" />
5670
<Reference Include="System.Web.Extensions" />
5771
<Reference Include="System.Xml.Linq" />
5872
<Reference Include="System.Drawing" />
@@ -81,10 +95,6 @@
8195
<Private>True</Private>
8296
<HintPath>..\packages\WebGrease.1.5.2\lib\WebGrease.dll</HintPath>
8397
</Reference>
84-
<Reference Include="Newtonsoft.Json">
85-
<Private>True</Private>
86-
<HintPath>..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
87-
</Reference>
8898
<Reference Include="Microsoft.AspNet.Web.Optimization.WebForms">
8999
<Private>True</Private>
90100
<HintPath>..\packages\Microsoft.AspNet.Web.Optimization.WebForms.1.1.3\lib\net45\Microsoft.AspNet.Web.Optimization.WebForms.dll</HintPath>

0 commit comments

Comments
 (0)