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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

how to deploy pbix file from particular path to report server using C#

 i have a powerbi report in particular folder in E: drive. How can i deploy it into powerbi report server url using C# code. I used to deploy it directly from powerbi file like opening it and using option save as : powerbi report server.

or simply upload it to report server using upload option.

now, i got a requirement to deploy the file using C# code. As i'm pretty new to c#.net , initially i referred some blogs and found some code for it. i faced few issues and could able tor resolve some of them. now, still i'm facing some Access to the path"E:\..." denied, and

An unhandled exception of type 'System.Net.WebException' occurred in System.dll

how can i resolve this and deploy my solution to report server. please help..

Code for PowerbiUploader.cs is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
using System.Threading.Tasks;

namespace ConsoleApplication1
{


    static internal class PowerBiUploader
    {
        public static WebResponse UploadPbiReport(string Path, string filename, string targetName, string targetPath)
        {
            // string apiString = String.Format("http://{0}/reports/api/v2.0", server);
            
            string apiString = Path;

            string cookie = GetCookie(apiString);

            string nonce = Extract(cookie, "XSRF-NONCE=");
            string token = ExtractToken(cookie, "XSRF-TOKEN=");

            return PostContent(filename, targetName, targetPath, apiString, nonce, token);
        }

        private static WebResponse PostContent(string filename, string targetName, string targetPath, string apiString,
            string nonce, string token)
        {
            string cookie;
            WebRequest webRequest = WebRequest.Create(apiString + "/CatalogItems");
            cookie = String.Format("XSRF-NONCE={0}; XSRF-TOKEN={1};", nonce, token);

            webRequest.Headers.Add("Cookie", cookie);
            webRequest.Headers.Add("X-XSRF-TOKEN", token);
            webRequest.Method = "POST";

            string content = Convert.ToBase64String(File.ReadAllBytes(filename));

            string postData = GetRequestData(targetName, targetPath, content);

            byte[] data = Encoding.ASCII.GetBytes((string)postData);

            webRequest.ContentType = "application/json;charset=utf-8";
            webRequest.ContentLength = data.Length;
            webRequest.UseDefaultCredentials = true;

            using (Stream stream = webRequest.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }

            return (HttpWebResponse)webRequest.GetResponse();
        }

        private static string GetCookie(string apiString)
        {
            WebRequest webRequest = WebRequest.Create(apiString );
            NetworkCredential cr = new NetworkCredential("windows username", "password");
            webRequest.Credentials = cr;
            //webRequest.UseDefaultCredentials = true;

            WebResponse webResponse = webRequest.GetResponse();

            string cookie = webResponse.Headers.Get("Set-Cookie");
            return cookie;
        }

        public static string Extract(string cookie, string tag)
        {
            int start = cookie.IndexOf(tag);
            string value = cookie.Substring(start + tag.Length);
            int end = value.IndexOf(";");
            string trimmed = value.Substring(0, end);
            return trimmed;

        }

        public static string ExtractToken(string cookie, string tag)
        {
            return WebUtility.HtmlDecode(Extract(cookie, tag));
        }

        public static string GetRequestData(string targetName, string targetPath, string content)
        {
            string template = @"

            { ""@odata.context"":""http://servername/Reports/"",""@odata.type"":""#Model.PowerBIReport"",""Id"":""00000000-0000-0000-0000-000000000000"",""Name"":""$$Name$$"",""Description"":null,""Path"":""$$Path$$"",""Type"":""PowerBIReport"",""Hidden"":false,""Size"":0,""ModifiedBy"":null,""ModifiedDate"":""0001-01-01T00:00:00Z"",""CreatedBy"":null,""CreatedDate"":""0001-01-01T00:00:00Z"",""ParentFolderId"":null,""ContentType"":null,""Content"":""$$Content$$"",""Properties"":[
              {
      ""Name"":""IsMobileOptimized"",""Value"":""False""
    },{
      ""Name"":""PbixShredderVersion"",""Value"":""1""
    }
  ],""IsFavorite"":false,""HasDataSources"":false
}";
            return template.Replace("$$Name$$", targetName)
                .Replace("$$Content$$", content)
                .Replace("$$Path$$", targetPath);
        }
    }
}
    

 

Code for programs.cs is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //var server = args[0];
            //var filename = args[1];
            //var targetPath = args[2];
           
               var Path = @"E:\New\pbix file deployment to url\pbix file deployment to url";
                          
            var filename = "First Report";
            var targetPath = "http://servername/Reports/";
            var targetName = "First Report";//: Path.GetFileNameWithoutExtension(args[1]); 

            PowerBiUploader.UploadPbiReport(Path, filename, targetName, targetPath);
        }
    }
}

can any please help.. big urgent..

thanx in advance..

1 REPLY 1
v-jiascu-msft
Employee
Employee

Hi @Anonymous,

 

Please refer to this post. It seems there isn't a direct way for now.

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors