Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Publish RDL Files To SSRS using C#

0.00/5 (No votes)
8 Apr 2018 1  
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here