public string RESTImportReport(string targetGroupName, string targetReportName, byte[] file) { PBIGroup group = RESTGetGroupByName(targetGroupName); //Import string importUrl = PBIConfig.APIbaseURL + String.Format("groups/{0}/imports?datasetDisplayName={1}&nameConflict=Abort", group.id, targetReportName); string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); HttpWebRequest importRequest = (HttpWebRequest)WebRequest.Create(importUrl); importRequest.ContentType = "multipart/form-data; boundary=" + boundary; importRequest.Method = "POST"; importRequest.KeepAlive = true; importRequest.Headers.Add("Authorization", String.Format("Bearer {0}", TokenService.FetchToken())); using (Stream rs = importRequest.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, "testdataset"); byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); rs.Write(headerbytes, 0, headerbytes.Length); rs.Write(file, 0, file.Length); byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); rs.Write(trailer, 0, trailer.Length); } HttpWebResponse importResponse = null; try { importResponse = (HttpWebResponse)importRequest.GetResponse(); } catch (WebException e) { using (WebResponse response = e.Response) { HttpWebResponse httpResponse = (HttpWebResponse)response; Console.WriteLine("Error code: {0}", httpResponse.StatusCode); using (Stream data = response.GetResponseStream()) using (var reader = new StreamReader(data)) { string text = reader.ReadToEnd(); Console.WriteLine(text); } } } Stream importResponseStream = importResponse.GetResponseStream(); string importResponseContent = (new StreamReader(importResponseStream)).ReadToEnd(); return(importResponseContent); }
public PBIReport RESTGetReportByName(string groupName, string reportName) { PBIGroup group = RESTGetGroupByName(groupName); string responseContent = RESTWrapper(String.Format("groups/{0}/Reports?$filter=name eq '{1}'", group.id, reportName)); PBIReports pbiReports = JsonConvert.DeserializeObject <PBIReports>(responseContent); return(pbiReports.List[0]); }
public byte[] RESTExportReport(string sourceGroupName, string sourceReportName) { PBIGroup group = RESTGetGroupByName(sourceGroupName); PBIReport report = RESTGetReportByName(sourceGroupName, sourceReportName); //Export .pbix file from source workspace, to create dataset in the target workspace string exportUrl = String.Format(PBIConfig.APIbaseURL + "groups/{0}/reports/{1}/Export", group.id, report.id); HttpWebRequest exportRequest = (HttpWebRequest)WebRequest.Create(exportUrl); exportRequest.ContentLength = 0; exportRequest.Method = "GET"; exportRequest.Headers.Add("Authorization", String.Format("Bearer {0}", TokenService.FetchToken())); HttpWebResponse exportResponse = (HttpWebResponse)exportRequest.GetResponse(); Stream exportResponseStream = exportResponse.GetResponseStream(); MemoryStream output = new MemoryStream(); exportResponseStream.CopyTo(output); byte[] file = output.ToArray(); return(file); }