Click here to Skip to main content
Click here to Skip to main content
Go to top

Reports Publisher

, 2 Jan 2013
Rate this:
Please Sign up or sign in to vote.
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:

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. The Button click event to the Publish Button is as follows:
  3. 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
        }
    }
  4. Creating folders in Reporting Service is as follows:
  5. 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");
        }
    }
  6. After creating folders, we need to upload Reports, Datasets, and Datastores and the process is shown below:
    1. Uploading the Reports
    2. 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. 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. 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)

Share

About the Author

Christopher Hasanali
Software Developer TCS
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberJacquesy221-May-14 17:11 
GeneralMy vote of 5 PinmemberLGiraldo20-Apr-13 23:03 
Questionaaa PinmemberLGiraldo20-Apr-13 23:00 
AnswerRe: aaa PinmemberChristopher Hasanali19-Nov-13 1:41 
GeneralPlease attached the source code PinmemberAbdullah Al Mamun Aakash27-Feb-13 13:57 
GeneralRe: Please attached the source code PinmemberChristopher Hasanali19-Nov-13 1:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 2 Jan 2013
Article Copyright 2013 by Christopher Hasanali
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid