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
{
public FileInfo[] GetReportsFiles(string ReportsFolderPath)
{
DirectoryInfo d = new DirectoryInfo(ReportsFolderPath);
FileInfo[] Files = d.GetFiles("*.rdl");
return Files;
}
public void PublishReports(FileInfo[] reports, ref ReportingService.ReportingService2010 rsc)
{
rsc.Credentials = System.Net.CredentialCache.DefaultCredentials;
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
{
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 {
Console.WriteLine(" ***** ******* Start Publishing Reports to Report Server ... ");
Console.WriteLine(string.Format("\n"));
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();
var ReportFiles = Publisher.GetReportsFiles
(ConfigurationSettings.AppSettings["ReportsFolderPath"]);
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.