Click here to Skip to main content
15,867,939 members
Articles / Database Development / SQL Server
Tip/Trick

Publish RDL Files To SSRS using C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
8 Apr 2018CPOL 28.2K   6   14
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:

XML
<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.

C#
 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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer ATI Systems
Egypt Egypt
I'm passionate ,motivated , interested in Software Engineering Mainly Microsoft technologies
I enjoy Solving complex challenges in addition to optimal solutions that meets users satisfaction with less resources .

Comments and Discussions

 
QuestionHow to publish the reports to a specific folder in Report Server Pin
pmak8-Aug-22 7:49
pmak8-Aug-22 7:49 
QuestionCan your code publish other types of files? Pin
Michael Ecklin10-Apr-18 8:53
Michael Ecklin10-Apr-18 8:53 
AnswerRe: Can your code publish other types of files? Pin
Sameh Abdel Mongy 10-Apr-18 9:27
Sameh Abdel Mongy 10-Apr-18 9:27 
QuestionCan the URL be changed dinamically? Pin
abacrotto9-Apr-18 7:23
abacrotto9-Apr-18 7:23 
AnswerRe: Can the URL be changed dinamically? Pin
Sameh Abdel Mongy 10-Apr-18 8:03
Sameh Abdel Mongy 10-Apr-18 8:03 
GeneralRe: Can the URL be changed dinamically? Pin
abacrotto10-Apr-18 8:13
abacrotto10-Apr-18 8:13 
GeneralRe: Can the URL be changed dinamically? Pin
Sameh Abdel Mongy 10-Apr-18 8:23
Sameh Abdel Mongy 10-Apr-18 8:23 
GeneralRe: Can the URL be changed dinamically? Pin
abacrotto10-Apr-18 8:31
abacrotto10-Apr-18 8:31 
GeneralRe: Can the URL be changed dinamically? Pin
Sameh Abdel Mongy 10-Apr-18 9:09
Sameh Abdel Mongy 10-Apr-18 9:09 
QuestionHow to publish rdl report to report server Pin
Mou_kol8-Apr-18 23:13
Mou_kol8-Apr-18 23:13 
AnswerRe: How to publish rdl report to report server Pin
Sameh Abdel Mongy 10-Apr-18 8:07
Sameh Abdel Mongy 10-Apr-18 8:07 
GeneralRe: How to publish rdl report to report server Pin
Mou_kol11-Apr-18 3:36
Mou_kol11-Apr-18 3:36 
GeneralRe: How to publish rdl report to report server Pin
Sameh Abdel Mongy 11-Apr-18 21:57
Sameh Abdel Mongy 11-Apr-18 21:57 

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

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