Forum Discussion

Amritha's avatar
Amritha
New Member
8 years ago
Solved

Error while importing .pbix using powerbi-client

TypeError: Cannot read property 'importState' of undefined at C:\Users\Admin\AppData\Roaming\npm\node_modules\powerbi-cli\lib\cli-impor t.js:69:70 at C:\Users\Admin\AppData\Roaming\npm\node_m...
  • Eric_Zhang's avatar
    Eric_Zhang
    8 years ago

    Amritha

    If you're using the NodeJS Power BI SDK, it is no longer supported. Try to directly call the REST API instead.

    I don't know NodeJS, you can reference how I call the Create Import REST API in C#.

     

     static void Main(string[] args)
            {
                //Import sample 
                string pbixPath = @"C:\test\2.pbix";
                string datasetDisplayName = "mydataset";
    
                string importResponse = Import(string.Format("{0}/groups/{1}/imports?datasetDisplayName={2}", datasetsBetaUri, groupId, datasetDisplayName), pbixPath,"Bearer Token here");
                  
                Console.WriteLine(importResponse);
    
            }
    
            public static string Import(string url, string fileName,string bearerToken)
            {
                string responseStatusCode = string.Empty;
    
                string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
                byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
    
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    
                request.ContentType = "multipart/form-data; boundary=" + boundary;
                request.Method = "POST";
                request.KeepAlive = true;
                request.Headers.Add("Authorization", String.Format("Bearer {0}", bearerToken));
    
                using (Stream rs = request.GetRequestStream())
                {
                    rs.Write(boundarybytes, 0, boundarybytes.Length);
    
                    string headerTemplate = "Content-Disposition: form-data; filename=\"{0}\"\r\nContent-Type: application / octet - stream\r\n\r\n";
                    string header = string.Format(headerTemplate, fileName);
                    byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
                    rs.Write(headerbytes, 0, headerbytes.Length);
    
                    using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                    {
                        byte[] buffer = new byte[4096];
                        int bytesRead = 0;
                        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            rs.Write(buffer, 0, bytesRead);
                        }
                    }
    
                    byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
                    rs.Write(trailer, 0, trailer.Length);
                }
                using (HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse)
                {
                    responseStatusCode = response.StatusCode.ToString();
                }
                             return responseStatusCode;
            }