1-
2- using System ;
1+ using System ;
2+ using System . Linq ;
33using System . Web ;
44using System . Web . UI ;
55using System . Collections . Specialized ;
6- using Newtonsoft . Json ;
76using PBIWebApp . Properties ;
87using Microsoft . IdentityModel . Clients . ActiveDirectory ;
8+ using Microsoft . PowerBI . Api . V2 ;
9+ using Microsoft . PowerBI . Api . V2 . Models ;
10+ using Microsoft . Rest ;
911
1012namespace 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}
0 commit comments