Exemplo n.º 1
0
        /// <summary>
        /// Takes a folder of reports, convert them and upload them to PBI workspace.
        /// </summary>
        /// <param name="urlEndPoint">the end point of report server.</param>
        /// <param name="inputPath">The Path of input Folder.</param>
        /// <param name="workspaceName">The name of requesting workspace.</param>
        /// <param name="clientID">The clientID of the App registered with permissions of Reading/Writing dataset, report and Reading Workspaces.</param>
        public void ConvertFolder(string urlEndPoint, string inputPath, string workspaceName, string clientID)
        {
            Trace("Starting the log-in window");
            PowerBIClientWrapper powerBIPortal = new PowerBIClientWrapper(workspaceName, clientID);

            Trace("Log-in successfully, retrieving the reports...");

            RdlFileIO rdlFileIO = new RdlFileIO(urlEndPoint);

            Trace($"Starting conversion and uploading the reports {DateTime.UtcNow.ToString()}");

            if (!Directory.Exists("output"))
            {
                Directory.CreateDirectory("output");
            }

            if (!rdlFileIO.IsFolder(inputPath))
            {
                ConvertAndUploadReport(
                    powerBIPortal,
                    rdlFileIO,
                    inputPath);
            }
            else
            {
                var reportPaths = rdlFileIO.GetReportsInFolder(inputPath);

                Console.WriteLine($"Found {reportPaths.Length} reports to convert");
                Parallel.ForEach(reportPaths, reportPath => ConvertAndUploadReport(
                                     powerBIPortal,
                                     rdlFileIO,
                                     reportPath));
            }
        }
Exemplo n.º 2
0
        private void ConvertAndUploadReport(PowerBIClientWrapper powerBIClient, RdlFileIO rdlFileIO, string reportPath)
        {
            var reportName = Path.GetFileName(reportPath);
            var report     = rdlFileIO.DownloadRdl(reportPath);

            SaveAndCopyStream(reportName, report, $"output\\{reportName}_original.rdl");

            if (powerBIClient.ExistReport(reportName))
            {
                Trace($"CONFLICT : {reportName}  A report with the same name already exists in the workspace");
            }
            else
            {
                try
                {
                    XElement[]   dataSets    = rdlFileIO.GetDataSets(reportPath, out Dictionary <KeyValuePair <string, string>, XElement> referenceDataSetMap);
                    DataSource[] dataSources = rdlFileIO.GetDataSources(reportPath);

                    var convertedFile = ConvertFile(reportPath, report, dataSources, referenceDataSetMap);
                    SaveAndCopyStream(reportName, convertedFile, $"output\\{reportName}_convert.rdl");

                    powerBIClient.UploadRDL(reportName + ReportFileExtension, convertedFile);

                    Trace($"SUCCESS : {reportName}  The file is successfully uploaded");
                }
                catch (HttpOperationException httpException)
                {
                    string errorMessage;
                    string requestId = String.Empty;
                    if (httpException?.Response?.Headers.ContainsKey("RequestId") == true)
                    {
                        requestId = httpException.Response.Headers["RequestId"].First();
                    }

                    if (httpException.Response.Headers.TryGetValue("X-PowerBI-Error-Details", out IEnumerable <string> returnedJsonStr))
                    {
                        if (returnedJsonStr.Count() != 1)
                        {
                            Trace($"FAILED TO UPLOAD : {reportName} RequestId:{requestId} {httpException.Message}");
                        }
                        else
                        {
                            string jsonString         = returnedJsonStr.First();
                            var    returnedJsonDetail = JObject.Parse(jsonString);
                            errorMessage = returnedJsonDetail["error"]["pbi.error"]["details"][2]["detail"]["value"].Value <string>();
                            Trace($"FAILED TO UPLOAD :  {reportName} RequestId:{requestId} {errorMessage}");
                        }
                    }
                    else
                    {
                        Trace($"FAILED TO UPLOAD : {reportName} RequestId:{requestId} {httpException.Message}");
                    }
                }
                catch (Exception e)
                {
                    Trace($"FAILED : {reportName} {e.Message}");
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Takes a folder of reports, convert them and upload them to PBI workspace.
        /// </summary>
        /// <param name="urlEndPoint">the end point of report server.</param>
        /// <param name="inputPath">The Path of input Folder.</param>
        /// <param name="workspaceName">The name of requesting workspace.</param>
        /// <param name="clientID">The clientID of the App registered with permissions of Reading/Writing dataset, report and Reading Workspaces.</param>
        public void ConvertFolder(string urlEndPoint, string inputPath, string workspaceName, string clientID)
        {
            Trace("Starting the log-in window");
            PowerBIClientWrapper powerBIClient = new PowerBIClientWrapper(workspaceName, clientID);

            Trace("Log-in successfully, retrieving the reports...");

            rdlFileIO = new RdlFileIO(urlEndPoint);

            Trace($"Starting conversion and uploading the reports {DateTime.UtcNow.ToString()}");

            if (!Directory.Exists("output"))
            {
                Directory.CreateDirectory("output");
            }

            if (!rdlFileIO.IsFolder(inputPath))
            {
                rootFolder = Path.GetDirectoryName(inputPath).Replace("\\", "/");
                var reportName = Path.GetFileName(inputPath);
                reportNameMap.TryAdd(reportName, 1);
                reportPaths.Enqueue(inputPath);
            }
            else
            {
                rootFolder = inputPath;
                var rootReports = rdlFileIO.GetReportsInFolder(inputPath);
                Console.WriteLine($"Found {rootReports.Length} reports to convert");
                foreach (string reportPath in reportPaths)
                {
                    var reportName = Path.GetFileName(reportPath);
                    reportNameMap.TryAdd(reportName, 1);
                }
                rootReports.ToList().ForEach(reportPaths.Enqueue);
            }
            while (reportPaths.Count > 0)
            {
                string reportPath = reportPaths.Dequeue();
                ConvertAndUploadReport(
                    powerBIClient,
                    rdlFileIO,
                    reportPath);
            }
        }