Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Reports Publisher

4.27/5 (4 votes)
2 Jan 2013CPOL1 min read 31.3K   418  
A tool used for publishing Microsoft Reports to a Microsoft WebService.

Introduction

Microsoft Web service provides us to upload Reports (*.rdl files) along with the data-sets and data-stores required for the reports to view. But when the reports (*.rdl files) and data-sets (*.rsd) are more .. the time taken to upload all the files and link report files to data-sets and later linking all data-sets to a data-source will take time. Hence this Report Publisher tool will create a Windows interface and by just clicking the button, the above mentioned activities can be done in a simple way.

Background

Background knowledge before going forward requires:

  • Web services
  • Microsoft's Reporting Services and functions
  • Reports, data sets, and data source
  • Windows application in Visual Studio

Using the code 

Using this code, we can upload reports to Reporting Services of Microsoft in a faster way. The following are the classes used for this application:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Web.Services.Protocols;
using Microsoft.SqlServer.ReportingServices2010;

Steps to create this tool:

  1. Create a Windows application in Microsoft Visual Studio and create a graphical Windows Form interface as below.
  2. Image 1

  3. The Button click event to the Publish Button is as follows:
  4. C#
    private void btnPublish_Click(object sender, EventArgs e)
    {
        lblErrorMsg.Text = String.Empty;
                 
        if (flag && String.IsNullOrEmpty(txtDataSource.Text))
        {
            lblErrorMsg.Text = "Please select DataSource path !!";
            flag = false;
        }
        if (flag && String.IsNullOrEmpty(txtDataSets.Text))
        {
            lblErrorMsg.Text = "Please select DataSets path !!";
            flag = false;
        }
        if (flag && String.IsNullOrEmpty(txtReports.Text))
        {
            lblErrorMsg.Text = "Please select Reports path !!";
            flag = false;
        }
        if (flag && String.IsNullOrEmpty(txtServer.Text))
        {
            lblErrorMsg.Text = "Please select TargetServer path !!";
            flag = false;
        }
        if (flag)
        {               
            lblprogess.Visible = true;
            progressBar1.Visible = true;
            progressBar1.Show();
            progressBar1.Step = 20;
           
            ReportingService2010 rs = new ReportingService2010(); 
            // Example for http://com:8080/abcd/ReportService2010.asmx\
            //rs.Url=txtServer.Text+"/"+"ReportService2010.asmx";
            rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
            CreateFolders(rs);
            progressBar1.PerformStep();
            CreateDataSource(rs);
            progressBar1.PerformStep();
            createDataSet(rs, txtDataSets.Text);
            progressBar1.PerformStep();
            createReport(rs, txtReports.Text);
            progressBar1.PerformStep();
            moveFiles(rs);
            progressBar1.PerformStep();
            lblprogess.Text = "";
            progressBar1.Hide();
            lblprogess.Visible = false;
            progressBar1.Visible = false;
            SaveIntoFile();
    
        }
        else
        {
          //do something you wish
        }
    }
  5. Creating folders in Reporting Service is as follows:
  6. C#
    private void CreateFolders(ReportingService2010 rs)
    {
        createFolder(rs, "/", "abcd");
        createFolder(rs, "/abcd", "abcd_REPORTS");
        createFolder(rs, "/abcd", "DATASETS");
        createFolder(rs, "/abcd", "abcd_ODS");
    }
    
    private void createFolder(ReportingService2010 rs,string folderPath, string folderName)
    {
        richTextBox1.SelectionColor  = Color.Green;
        
        richTextBox1.AppendText("\n["+DateTime.Now.ToString(
           "yyyy-MM-dd HH:mm:ss.fff")+"] Creating folder : " + folderName);
        try
        {
            rs.CreateFolder(folderName, folderPath, null);
            richTextBox1.SelectionColor  = Color.Green;
            richTextBox1.AppendText("\n["+DateTime.Now.ToString(
              "yyyy-MM-dd HH:mm:ss.fff")+"] Folder created: "+ folderName + " Successfully");
        }
    
        catch (Exception e)
        {
            richTextBox1.SelectionColor  = Color.Red;
            richTextBox1.AppendText("\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
               "] Error while creating folder : " + folderName);
            richTextBox1.AppendText("\n" + e.Message+"\n");
        }
    }
  7. After creating folders, we need to upload Reports, Datasets, and Datastores and the process is shown below:
    1. Uploading the Reports
    2. C#
      private void createReport(ReportingService2010 rs,string reportPath)
      {
          foreach (string fileName in Directory.GetFiles(reportPath,"*.rdl"))
          {
              createReportInServer(rs,Path.GetFileNameWithoutExtension(fileName));
        }
      }
      
      private void createReportInServer(ReportingService2010 rs,string reportName)
      {
          Byte[] definition = null;
          Warning[] warnings = null;
          try
          {
              
              FileStream stream = File.OpenRead(Path.Combine(txtReports.Text,reportName+".rdl"));
              definition = new Byte[stream.Length];
              stream.Read(definition, 0, (int)stream.Length);
              stream.Close();
          }
          catch (Exception e)
          {
              richTextBox1.SelectionColor  = Color.Red;
              richTextBox1.AppendText("\n[" + DateTime.Now.ToString(
                "yyyy-MM-dd HH:mm:ss.fff") + "] Error while reading report : " + reportName);
              richTextBox1.AppendText("\n\n" + e.Message+"\n");
          }
      
          try
          {
              string parent = "/abcd/abcd_ODS";
              CatalogItem report = rs.CreateCatalogItem("Report", reportName, parent,
                          true, definition, null, out warnings);
              
              if (warnings != null)
              {
                  foreach (Warning warning in warnings)
                  {
                      richTextBox1.SelectionColor  = Color.Violet;
                      richTextBox1.AppendText("\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
                        "] Warning while creating report : " + reportName);
                      richTextBox1.AppendText("\n" + warning.Message+"\n");
                  }
              }
                  
              else
              {
                  richTextBox1.SelectionColor  = Color.Green;
                  richTextBox1.AppendText("\n[" + 
                    DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
                    "] Report: " + reportName + " created successfully with no warnings");
              }
          }
          catch (Exception e)
          {
              richTextBox1.SelectionColor  = Color.Red;
              richTextBox1.AppendText("\n\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
                 "] Error while creating report : " + reportName);
              richTextBox1.AppendText("\n" + e.Message);
          }
         
      }
    3. Creating of data sets in Reporting Service:
    4. C#
      private void createDataSet(ReportingService2010 rs, string datasetPath)
      {
          foreach (string fileName in Directory.GetFiles(datasetPath,"*.rsd"))
          {
              createDataSetInServer(rs, Path.GetFileNameWithoutExtension(fileName));
          }
      }
      
      private void createDataSetInServer(ReportingService2010 rs, string DataSetName)
      {
          Byte[] definition = null;
          Warning[] warnings = null;
      
          try
          {
      
              FileStream stream = File.OpenRead(Path.Combine(txtDataSets.Text, DataSetName+".rsd"));
              definition = new Byte[stream.Length];
              stream.Read(definition, 0, (int)stream.Length);
              stream.Close();
          }
          catch (Exception e)
          {
              richTextBox1.SelectionColor  = Color.Red;
              richTextBox1.AppendText("\n\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
                "] Error while reading Dataset : " + DataSetName);
              richTextBox1.AppendText("\n" + e.Message);
          }
          try
          {
              string parent = "/abcd/abcd_ODS";
              CatalogItem dataset = rs.CreateCatalogItem("DataSet", DataSetName, parent,
                          true, definition, null, out warnings);
              if (warnings != null)
              {
                  foreach (Warning warning in warnings)
                  {
                      richTextBox1.SelectionColor  = Color.Violet;
                      richTextBox1.AppendText("\n[" + DateTime.Now.ToString(
                        "yyyy-MM-dd HH:mm:ss.fff") + "] Warning while creating dataset : " + DataSetName);
                      richTextBox1.AppendText("\n" + warning.Message+"\n");
                  }
              }
              else
              {
                  richTextBox1.SelectionColor  = Color.Green;
                  richTextBox1.AppendText("\n[" + DateTime.Now.ToString(
                    "yyyy-MM-dd HH:mm:ss.fff") + "] DataSet: " + 
                    DataSetName + " created successfully with no warnings");
              }
          }
          catch (Exception e)
          {
              richTextBox1.SelectionColor  = Color.Red;
              richTextBox1.AppendText("\n[" + 
                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
                "] Error while creating dataset : " + DataSetName);
              richTextBox1.AppendText("\n"+e.Message+"\n");
          }
      }
    5. Creating Reports Data Source and binding to SQL Server.
    6. C#
      private void CreateDataSource(ReportingService2010 rs)
      {
          richTextBox1.SelectionColor  = Color.Green;
          richTextBox1.AppendText("\n[" + DateTime.Now.ToString(
            "yyyy-MM-dd HH:mm:ss.fff") + "] Creating dataSource....");
              
          string parent = "/abcd/abcd_ODS";
          string name = "abcd_ODS";
       // Define the data source definition.
          DataSourceDefinition definition = new DataSourceDefinition();
          definition.CredentialRetrieval = CredentialRetrievalEnum.Integrated;
          definition.ConnectString = "Data Source=comp_name\\abcd;Initial Catalog=abcd_ODS";
          definition.Enabled = true;
          definition.EnabledSpecified = true;
          definition.Extension = "SQL";
          definition.ImpersonateUserSpecified = false;
          
          definition.WindowsCredentials = true;
      
          try
          {
              rs.CreateDataSource(name, parent, true, definition, null);
              richTextBox1.SelectionColor  = Color.Green;
              richTextBox1.AppendText("\n[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + 
                "] Data Source " + name + " created at " + parent + " Successfully !!");
          }
      
          catch (Exception ex)
          {
              richTextBox1.SelectionColor  = Color.Red;
              richTextBox1.AppendText("\n[" + DateTime.Now.ToString(
                "yyyy-MM-dd HH:mm:ss.fff") + "] Error While creating DATASOURCE..\n");
              richTextBox1.AppendText(ex.Message);
              richTextBox1.AppendText("\n");
          }
      }

Points of Interest 

Learning how Windows Reporting Services operates manually and creating a tool coded for avoiding the manual uploads.

History 

Note: Please find the attachment for complete code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)