Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
michael21
Regular Visitor

Not able to import pbix file programmtically

Hi All,

 

I am not able to upload PBIX file in workspace. 

First ,

1. I created Workspace collection manually.

2. Created workspace programmatically and its returne  the workspace id.issue.png

 

3.  Add the pbix file using programmatically

  1. i get file using HttpPosted file and upload to the blob.

   2. Download the from the blob as a stream  and upload the power bi workspace.

I used PostImportWithFileAsync function. 

i got "Operation returned an invalid status code 'BadRequest'" type Microsoft.Rest.HttpOperationException

Please help.

  

1 ACCEPTED SOLUTION
Eric_Zhang
Employee
Employee

@michael21

Below demo works in my test. I'd guess it might be the incorrect stream that causes the error? Before using a blob stream, instead testing with a local file at first.

 

 

using System;
using System.Collections.Generic;
using System.Linq;

using System.Threading.Tasks; 
//Install-Package Microsoft.PowerBI.Api
using Microsoft.PowerBI.Api.V1;
using Microsoft.PowerBI.Api.V1.Models; 
using System.IO;
using System.Threading;
using Microsoft.Rest;

namespace importPbix_PBIE
{
    class Program
    {


        static string clientId = "49df1bc7-db68-4fb4-91c0-6d93f770d1a4";

        //power bi workspace collection accesskey
        static string accessKey = "KJixsmmwxxxxxxxiXY8iE/zNVHROCmcaOIFr6a2vmQ==";
        static string workspaceCollectionName = "cixxsxxdemo";
        static string workspaceId = "79c71xxxxb79d2xxxxe0b";
        static string filePath = @"C:\test\test.pbix";


        static void Main(string[] args)
        {
            var task = ImportPbix(workspaceCollectionName, workspaceId, "mytestdataset", filePath);
            task.Wait();
            Console.ReadKey();
        }

        static async Task<Import> ImportPbix(string workspaceCollectionName, string workspaceId, string datasetName, string filePath)
        {
            using (var fileStream = File.OpenRead(filePath.Trim('"')))
            {
                using (var client = await CreateClient())
                {
                    // Set request timeout to support uploading large PBIX files
                    client.HttpClient.Timeout = TimeSpan.FromMinutes(60);
                    client.HttpClient.DefaultRequestHeaders.Add("ActivityId", Guid.NewGuid().ToString());

                    // Import PBIX file from the file stream
                    var import = await client.Imports.PostImportWithFileAsync(workspaceCollectionName, workspaceId, fileStream, datasetName);

                    // Example of polling the import to check when the import has succeeded.
                    while (import.ImportState != "Succeeded" && import.ImportState != "Failed")
                    {
                        import = await client.Imports.GetImportByIdAsync(workspaceCollectionName, workspaceId, import.Id);
                        Console.WriteLine("Checking import state... {0}", import.ImportState);
                        Thread.Sleep(1000);
                    }

                    return import;
                }
            }
        }


        static async Task<PowerBIClient> CreateClient()
        {
            // Create a token credentials with "AppKey" type
            var credentials = new TokenCredentials(accessKey, "AppKey");

            // Instantiate your Power BI client passing in the required credentials
            var client = new PowerBIClient(credentials);

            // Override the api endpoint base URL.  Default value is https://api.powerbi.com
            client.BaseUri = new Uri("https://api.powerbi.com");

            return client;
        }


    }
}

 

View solution in original post

2 REPLIES 2
Eric_Zhang
Employee
Employee

@michael21

Below demo works in my test. I'd guess it might be the incorrect stream that causes the error? Before using a blob stream, instead testing with a local file at first.

 

 

using System;
using System.Collections.Generic;
using System.Linq;

using System.Threading.Tasks; 
//Install-Package Microsoft.PowerBI.Api
using Microsoft.PowerBI.Api.V1;
using Microsoft.PowerBI.Api.V1.Models; 
using System.IO;
using System.Threading;
using Microsoft.Rest;

namespace importPbix_PBIE
{
    class Program
    {


        static string clientId = "49df1bc7-db68-4fb4-91c0-6d93f770d1a4";

        //power bi workspace collection accesskey
        static string accessKey = "KJixsmmwxxxxxxxiXY8iE/zNVHROCmcaOIFr6a2vmQ==";
        static string workspaceCollectionName = "cixxsxxdemo";
        static string workspaceId = "79c71xxxxb79d2xxxxe0b";
        static string filePath = @"C:\test\test.pbix";


        static void Main(string[] args)
        {
            var task = ImportPbix(workspaceCollectionName, workspaceId, "mytestdataset", filePath);
            task.Wait();
            Console.ReadKey();
        }

        static async Task<Import> ImportPbix(string workspaceCollectionName, string workspaceId, string datasetName, string filePath)
        {
            using (var fileStream = File.OpenRead(filePath.Trim('"')))
            {
                using (var client = await CreateClient())
                {
                    // Set request timeout to support uploading large PBIX files
                    client.HttpClient.Timeout = TimeSpan.FromMinutes(60);
                    client.HttpClient.DefaultRequestHeaders.Add("ActivityId", Guid.NewGuid().ToString());

                    // Import PBIX file from the file stream
                    var import = await client.Imports.PostImportWithFileAsync(workspaceCollectionName, workspaceId, fileStream, datasetName);

                    // Example of polling the import to check when the import has succeeded.
                    while (import.ImportState != "Succeeded" && import.ImportState != "Failed")
                    {
                        import = await client.Imports.GetImportByIdAsync(workspaceCollectionName, workspaceId, import.Id);
                        Console.WriteLine("Checking import state... {0}", import.ImportState);
                        Thread.Sleep(1000);
                    }

                    return import;
                }
            }
        }


        static async Task<PowerBIClient> CreateClient()
        {
            // Create a token credentials with "AppKey" type
            var credentials = new TokenCredentials(accessKey, "AppKey");

            // Instantiate your Power BI client passing in the required credentials
            var client = new PowerBIClient(credentials);

            // Override the api endpoint base URL.  Default value is https://api.powerbi.com
            client.BaseUri = new Uri("https://api.powerbi.com");

            return client;
        }


    }
}

 

Thanks Eric, i got it. I copy and paste your code, its working perfectly.

 

Thanks a lot.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.