how to deploy pbix file from particular path to report server url using RRS feed

  • Question

  • hi all,

        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 , 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"":[
                return template.Replace("$$Name$$", targetName)
                    .Replace("$$Content$$", content)
                    .Replace("$$Path$$", targetPath);

    Program.cs code 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\PBIXFileDeploymentToReportServer";
                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..


    Tuesday, April 24, 2018 6:57 AM

All replies

  • Hi LuckyLucky12,

    Thank you for posting here.

    For your question is more related to PowerBI, you could post a new thread in Power BI Community.

    The Visual C# forum discuss and ask questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Wednesday, April 25, 2018 8:33 AM