<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Not able to import pbix file programmtically in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Not-able-to-import-pbix-file-programmtically/m-p/210948#M6658</link>
    <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="35372" data-lia-user-login="michael21" class="lia-mention lia-mention-user"&gt;michael21&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Below demo works in my test. I'd guess it might be the incorrect stream that causes the error? &lt;STRONG&gt;Before using a blob stream, instead testing with a local file at first.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;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&amp;lt;Import&amp;gt; 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" &amp;amp;&amp;amp; 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&amp;lt;PowerBIClient&amp;gt; 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;
        }


    }
}&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 13 Jul 2017 03:24:35 GMT</pubDate>
    <dc:creator>Eric_Zhang</dc:creator>
    <dc:date>2017-07-13T03:24:35Z</dc:date>
    <item>
      <title>Not able to import pbix file programmtically</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Not-able-to-import-pbix-file-programmtically/m-p/210571#M6649</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am not able to upload PBIX file in workspace.&amp;nbsp;&lt;/P&gt;&lt;P&gt;First ,&lt;/P&gt;&lt;P&gt;1. I created Workspace collection manually.&lt;/P&gt;&lt;P&gt;2. Created workspace programmatically and its returne &amp;nbsp;the workspace id.&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. &amp;nbsp;Add the pbix file using programmatically&lt;/P&gt;&lt;P&gt;&amp;nbsp; 1. i get file using HttpPosted file and upload to the blob.&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;2. Download the from the blob as a stream &amp;nbsp;and upload the power bi workspace.&lt;/P&gt;&lt;P&gt;I used&amp;nbsp;PostImportWithFileAsync function.&amp;nbsp;&lt;/P&gt;&lt;P&gt;i got "Operation returned an invalid status code 'BadRequest'" type Microsoft.Rest.HttpOperationException&lt;/P&gt;&lt;P&gt;Please help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2017 13:24:42 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Not-able-to-import-pbix-file-programmtically/m-p/210571#M6649</guid>
      <dc:creator>michael21</dc:creator>
      <dc:date>2017-07-12T13:24:42Z</dc:date>
    </item>
    <item>
      <title>Re: Not able to import pbix file programmtically</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Not-able-to-import-pbix-file-programmtically/m-p/210948#M6658</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="35372" data-lia-user-login="michael21" class="lia-mention lia-mention-user"&gt;michael21&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Below demo works in my test. I'd guess it might be the incorrect stream that causes the error? &lt;STRONG&gt;Before using a blob stream, instead testing with a local file at first.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;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&amp;lt;Import&amp;gt; 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" &amp;amp;&amp;amp; 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&amp;lt;PowerBIClient&amp;gt; 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;
        }


    }
}&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2017 03:24:35 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Not-able-to-import-pbix-file-programmtically/m-p/210948#M6658</guid>
      <dc:creator>Eric_Zhang</dc:creator>
      <dc:date>2017-07-13T03:24:35Z</dc:date>
    </item>
    <item>
      <title>Re: Not able to import pbix file programmtically</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Not-able-to-import-pbix-file-programmtically/m-p/211432#M6670</link>
      <description>&lt;P&gt;Thanks Eric, i got it. I copy and paste your code, its working perfectly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2017 15:35:29 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Not-able-to-import-pbix-file-programmtically/m-p/211432#M6670</guid>
      <dc:creator>michael21</dc:creator>
      <dc:date>2017-07-13T15:35:29Z</dc:date>
    </item>
  </channel>
</rss>

