65.9K
CodeProject is changing. Read more.
Home

Publish RDL Files To SSRS using C#

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Apr 8, 2018

CPOL
viewsIcon

30489

Deploy Reports rdl files to SSRS using C# script

Introduction

This tip shows how to make a script to publish bulk of report files (.rdl) to report server for SQl Server 2012 using C#.

Prerequisites

Let's start ...

Using the Code

Add Web Service for Report server to the project.

Notice: We need to add it as Web Reference, not Service Reference.

Select References on the project.

Add Service Reference -> Advanced -> Add Web Reference

Our end point URL:

http://server/reportserver/reportservice2010.asmx?wsdl  

Configure Report folder in App.config:

  <appSettings>
    <add key="ReportsFolderPath" value="E:\Reports\"/> 
  </appSettings>   

Now your reference for Report server web service is added to your project.

We will use PublishManager Class.

   public class PublishManager
  {
    /// <summary>
    /// Get All Reports files From Reports Directory 
    /// </summary>
    /// <param name="ReportsFolderPath"></param>
    public FileInfo[] GetReportsFiles(string ReportsFolderPath)
    {
      DirectoryInfo d = new DirectoryInfo(ReportsFolderPath);//Assuming Test is your Folder
      FileInfo[] Files = d.GetFiles("*.rdl"); //Getting Text files
      return Files;
    }
    
    /// <summary>
    /// Deploy Reports to Reporting Server 
    /// </summary>
    /// <param name="reports">array of RDL Files </param>
    /// <param name="rsc">Reporting Service object</param>
    public void PublishReports(FileInfo[] reports, ref  ReportingService.ReportingService2010 rsc)
    {         
      rsc.Credentials = System.Net.CredentialCache.DefaultCredentials; //User credential for 
                                                                       //Reporting Service 
                                                                       //the current logged system user 
      Warning[] Warnings = null;
      foreach (FileInfo ReportFile in reports)
      {
        Byte[] definition = null;
        Warning[] warnings = null;

        try
        {
          FileStream stream = ReportFile.OpenRead();
          definition = new Byte[stream.Length];
          stream.Read(definition, 0, (int)stream.Length);
          stream.Close();
        }

        catch (IOException e)
        {
          Console.WriteLine(e.Message);
        }

        try
        {
          ///Creating Catalog of type Report in report server   
          ReportingService.CatalogItem report = rsc.CreateCatalogItem("Report", 
          ReportFile.Name, @"/", true, definition, null, out Warnings);
          if (report != null)
          {
            Console.WriteLine(ReportFile.Name + " Published Successfully ");
            Console.WriteLine(string.Format("\n"));         
          }
          if (warnings != null)
          {
            foreach (Warning warning in warnings)
            {
              Console.WriteLine(string.Format("Report: {0} has warnings", warning.Message));
              Console.WriteLine(string.Format("\n"));
            }
          }
          else
            Console.WriteLine(string.Format
                 ("Report: {0} created successfully with no warnings", ReportFile.Name));
          Console.WriteLine(string.Format("\n"));
        }

        catch (SoapException e)
        {
          Console.WriteLine(e.Detail.InnerXml.ToString());
          Console.WriteLine(string.Format("\n"));
        }
      }
    }
  }

and this Main for our tool:

   static void Main(string[] args)
        {
            try {  
            /// 1-we need to specify Reports Folder 
            /// 2- get report server url  
            /// 3-get All RDL Files 
            /// 4- publish Reports to report server 
            /// 5- confirm publishing 
           
            Console.WriteLine(" ***** ******* Start Publishing Reports to Report Server ... ");
            Console.WriteLine(string.Format("\n"));
           
           ///initialize instance for Reporting Service class 
            ReportingService2010 ReportingService = new ReportingService2010();
            ReportingService.Credentials = System.Net.CredentialCache.DefaultCredentials;

            Console.WriteLine(" Report Server URL :   " + ReportingService.Url);
            Console.WriteLine(string.Format("\n"));


            PublishManager Publisher = new PublishManager();

            ///Get All Files From Reports Directory 
            var ReportFiles =     Publisher.GetReportsFiles
                                  (ConfigurationSettings.AppSettings["ReportsFolderPath"]);

            ///Publish all files to Reporting Server 
            Publisher.PublishReports(ReportFiles, ref ReportingService);

            Console.WriteLine(" ALL Done Successfully... ");

            Console.WriteLine(" Press any Key for Exit....");

            Console.ReadKey();            
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }   

Hope everything is clear.

If you have any questions, please feel free to contact me using the comments section below.