Download CRM Reports using SDK

Download CRM Reports using SDK

I recently got a request to download all the report rdl files. Because of the number of reports it would take forever, so using the CRM SDK you can easily download rdl files.

I use generated entities but you can use dynamic entity values as well.

 

 

// Get list of all reports
 List<Report> reports = (from rp in service.ReportSet
                                  select new Report
                                  {
                                    ReportId = rp.ReportId,
                                    Name = rp.Name,
                                    Description = rp.Description,
                                    FileName = rp.FileName
                                  }).ToList();
          foreach (Report report in reports)
          {
// Use the path defined in a textbox on a windows form..
// You can also change this value to your destination            
string reportPath = Path.Combine(txtPath.Text, report.Name);
            filename = report.FileName;
            if (!Directory.Exists(reportPath))
            {
              Directory.CreateDirectory(reportPath);
            }
            // Use the Download Report Definition message.
            DownloadReportDefinitionRequest rdlRequest = new DownloadReportDefinitionRequest
            {
              ReportId = report.ReportId.Value
            };
            
            DownloadReportDefinitionResponse rdlResponse = (DownloadReportDefinitionResponse)service.Execute(rdlRequest);

            // Access the xml data and save to disk
            XmlTextWriter reportDefinitionFile = new XmlTextWriter(reportPath + "\\" + report.FileName, System.Text.Encoding.UTF8);
            reportDefinitionFile.WriteRaw(rdlResponse.BodyText);
            reportDefinitionFile.Close();

Email Newsletter

Missing out on the latest Cupofdev.com developments? Enter your email below to receive future announcements direct to your inbox. An email confirmation will be sent before you will start receiving notifications - please check your spam folder if you don't receive this.