Example #1
0
        //public IActionResult TimeConsuming()
        //{
        //    var uniqueName = HttpContext.User.Claims.First(claim => claim.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name").Value;

        //    UsageTelemetry.Track(uniqueName, ArdaUsage.Report_Show);

        //    return View();
        //}

        public async Task <IActionResult> TimeConsuming()
        {
            string userObjectID = HttpContext.User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
            var    tokenCache   = new NaiveSessionCache(userObjectID, NaiveSessionCacheResource.PowerBi, _cache);
            //ClientCredential credential = new ClientCredential(Startup.ClientId, Startup.ClientSecret);

            AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, tokenCache);

            var authenticationResult = await authContext.AcquireTokenSilentAsync(Startup.PowerBIResourceId, Startup.ClientId);

            var tokenCredentials = new TokenCredentials(authenticationResult.AccessToken, "Bearer");

            using (var client = new PowerBIClient(new Uri(Startup.PowerBIApiUrl), tokenCredentials))
            {
                // You will need to provide the GroupID where the dashboard resides.
                ODataResponseListReport reports = await client.Reports.GetReportsInGroupAsync(Startup.PowerBIGroupId);

                // Get the first report in the group.
                Report report = reports.Value.FirstOrDefault();

                // Generate Embed Token.
                var        generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
                EmbedToken tokenResponse = client.Reports.GenerateTokenInGroup(Startup.PowerBIGroupId, report.Id, generateTokenRequestParameters);

                // Generate Embed Configuration.
                var embedConfig = new EmbedConfig()
                {
                    EmbedToken = tokenResponse,
                    EmbedUrl   = report.EmbedUrl,
                    Id         = report.Id
                };

                return(View("PowerBI", embedConfig));
            }
        }
Example #2
0
        private Report NETGetReportByName(string groupName, string reportName)
        {
            Group group = NETGetGroupByName(groupName);
            ODataResponseListReport reports = ClientService.Client.Reports.GetReports(group.Id);

            return(reports.Value.First(x => x.Name.Equals(reportName)));
        }
        public async Task <EmbedToken> GetNewTokenResponse(string WorkspaceId, string ReportId, dynamic tokenCredentials)
        {
            EmbedToken tokenResponse;

            try
            {
                using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
                {
                    ODataResponseListReport reports = await client.Reports.GetReportsInGroupAsync(WorkspaceId);

                    Report report;
                    if (string.IsNullOrWhiteSpace(ReportId))
                    {
                        // Get the first report in the workspace.
                        report = reports.Value.FirstOrDefault();
                    }
                    else
                    {
                        report = reports.Value.FirstOrDefault(r => r.Id == ReportId);
                    }
                    var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
                    tokenResponse = await client.Reports.GenerateTokenInGroupAsync(WorkspaceId, report.Id, generateTokenRequestParameters);
                }

                return(tokenResponse);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Example #4
0
        public static async Task <IEnumerable <Report> > GetReportsInWorkspace(string workspaceId)
        {
            using (PowerBIClient client = await CreateClient())
            {
                ODataResponseListReport response = await client.Reports.GetReportsInGroupAsync(workspaceId);

                return(response.Value);
            }
        }
Example #5
0
        public async Task <string> GetReportUrlAsync(string groupId, string reportId)
        {
            var client = GetClient();
            ODataResponseListReport reports = await client.Reports.GetReportsInGroupAsync(groupId).ConfigureAwait(false);

            Microsoft.PowerBI.Api.V2.Models.Report report = reports.Value.FirstOrDefault(r => r.Id == reportId);

            return(report.EmbedUrl);
        }
Example #6
0
        /* .NET SDK Reference
         * https://docs.microsoft.com/en-us/dotnet/api/microsoft.powerbi.api.v2?view=azure-dotnet
         */

        /* Embedding resources
         * https://docs.microsoft.com/en-us/power-bi/developer/embed-sample-for-customers
         * https://github.com/Microsoft/PowerBI-Developer-Samples
         * https://microsoft.github.io/PowerBI-JavaScript/demo/v2-demo/index.html#
         */

        public void NETListReports()
        {
            ODataResponseListGroup groups = ClientService.Client.Groups.GetGroups();

            foreach (Group group in groups.Value)
            {
                Console.WriteLine(group.Name);
                ODataResponseListReport reports = ClientService.Client.Reports.GetReports(group.Id);

                foreach (Report report in reports.Value)
                {
                    Console.WriteLine("     " + report.Name);
                }
            }
        }
        public ActionResult Index()
        {
            // use app key to create credentials used to retreive developer access token
            TokenCredentials credentials = new TokenCredentials(accessKey, "AppKey");
            PowerBIClient    client      = new PowerBIClient(credentials);

            // call to Power BI REST API to get list of reports inside a specific worksapce
            ODataResponseListReport reportsResult = client.Reports.GetReportsAsync(workspaceCollection, workspaceId).Result;

            // this sample app is hardcoded to load first report in collection
            Report report = reportsResult.Value[0];

            // create the embed token for the report
            PowerBIToken embedToken = PowerBIToken.CreateReportEmbedToken(workspaceCollection, workspaceId, report.Id);

            // Pass report and access token to MVC view for rending
            ReportViewModel reportModel = new ReportViewModel {
                Report      = report,
                AccessToken = embedToken.Generate(accessKey)
            };

            return(View(reportModel));
        }
        static void Main(string[] args)
        {
            try
            {
                // Create a user password cradentials.
                credential = new UserPasswordCredential(username, Secrets.Password);

                // Authenticate using created credentials
                Authorize().Wait();

                using (var client = new PowerBIClient(new Uri(apiUrl), tokenCredentials))
                {
                    EmbedToken embedToken = client.Reports.GenerateTokenInGroup(groupId, "<REPORT ID>", new GenerateTokenRequest(accessLevel: "View", datasetId: "<DATASET ID>"));

                    Report report = client.Reports.GetReportInGroup(groupId, "<REPORT ID>");

                    #region Output Embed Token
                    Console.WriteLine("\r*** EMBED TOKEN ***\r");

                    Console.Write("Report Id: ");

                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine("<REPORT ID>");
                    Console.ResetColor();

                    Console.Write("Report Embed Url: ");

                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine(report.EmbedUrl);
                    Console.ResetColor();

                    Console.WriteLine("Embed Token Expiration: ");

                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine(embedToken.Expiration.Value.ToString());
                    Console.ResetColor();


                    Console.WriteLine("Report Embed Token: ");
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(embedToken.Token);
                    Console.ResetColor();
                    #endregion

                    #region Output Datasets
                    Console.WriteLine("\r*** DATASETS ***\r");

                    // List of Datasets
                    // This method calls for items in a Group/App Workspace. To get a list of items within your "My Workspace"
                    // call GetDatasets()
                    ODataResponseListDataset datasetList = client.Datasets.GetDatasetsInGroup(groupId);

                    foreach (Dataset ds in datasetList.Value)
                    {
                        Console.WriteLine(ds.Id + " | " + ds.Name);
                    }
                    #endregion

                    #region Output Reports
                    Console.WriteLine("\r*** REPORTS ***\r");

                    // List of reports
                    // This method calls for items in a Group/App Workspace. To get a list of items within your "My Workspace"
                    // call GetReports()
                    ODataResponseListReport reportList = client.Reports.GetReportsInGroup(groupId);

                    foreach (Report rpt in reportList.Value)
                    {
                        Console.WriteLine(rpt.Id + " | " + rpt.Name + " | DatasetID = " + rpt.DatasetId);
                    }
                    #endregion

                    #region Output Dashboards
                    Console.WriteLine("\r*** DASHBOARDS ***\r");

                    // List of reports
                    // This method calls for items in a Group/App Workspace. To get a list of items within your "My Workspace"
                    // call GetReports()
                    ODataResponseListDashboard dashboards = client.Dashboards.GetDashboardsInGroup(groupId);

                    foreach (Dashboard db in dashboards.Value)
                    {
                        Console.WriteLine(db.Id + " | " + db.DisplayName);
                    }
                    #endregion

                    #region Output Gateways
                    Console.WriteLine("\r*** Gateways ***\r");

                    ODataResponseListGateway gateways = client.Gateways.GetGateways();

                    Console.WriteLine(gateways.Value[0].Name);

                    //foreach (Gateway g in gateways)
                    //{
                    //    Console.WriteLine(g.Name + " | " + g.GatewayStatus);
                    //}
                    #endregion
                }
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.ToString());
                Console.ResetColor();
            }
        }