Code
using System;
using System.Web.Services.Protocols;
using ReportingService.ReportService;
using System.Xml;
using System.IO;
class Sample
{
public static string ConfigFile = "Config.xml";
public static void Main()
{
ReportingService2005 rs = new ReportingService2005();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ConfigFile);
string parentFolder = xmlDoc.SelectSingleNode("//ParentFolder").InnerText;
//check whether have the parentFolder
if (rs.GetItemType("/"+parentFolder)!=ItemTypeEnum.Folder)
{
try
{
rs.CreateFolder(parentFolder, "/", null);
Console.WriteLine("Parent folder {0} created successfully", parentFolder);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
XmlNodeList nodeList = xmlDoc.SelectNodes("//ReportName");
foreach (XmlNode node in nodeList)
{
PublishReport(parentFolder, node.InnerText , rs, xmlDoc);
}
}
/// <summary>
/// Create Report and set the Data source
/// </summary>
/// <param name="parentFolder"></param>
/// <param name="reportName"></param>
/// <param name="rs"></param>
/// <param name="xmlDoc"></param>
public static void PublishReport(string parentFolder, string reportName, ReportingService2005 rs, XmlDocument xmlDoc)
{
Byte[] definition = null;
Warning[] warnings = null;
string filePath = xmlDoc.SelectSingleNode("//FilePath").InnerText;
try
{
//read report definition
FileStream stream = File.OpenRead(Path.Combine(filePath,reportName+".rdl"));
definition = new Byte[stream.Length];
stream.Read(definition, 0, (int) stream.Length);
stream.Close();
}
catch(IOException e)
{
Console.WriteLine(e.Message);
}
try
{
//create report
warnings = rs.CreateReport(reportName,"/"+parentFolder, false, definition, null);
//set report data source
SetReportDataSources(parentFolder, reportName, xmlDoc, rs);
if (warnings != null)
{
foreach (Warning warning in warnings)
{
Console.WriteLine(warning.Message);
}
}
else
{
Console.WriteLine("Report: {0} created successfully with no warnings", reportName);
}
}
catch (SoapException e)
{
Console.WriteLine(e.Detail.InnerXml.ToString());
}
}
private static void SetReportDataSources(string parentFolder,string reportName,XmlDocument xmlDoc,ReportingService2005 rs)
{
string dataSourceFolder=xmlDoc.SelectSingleNode("//DataSourceFolder").InnerText;
string dataSourceName = xmlDoc.SelectSingleNode("//DataSourceName").InnerText;
DataSource[] dsarray;
DataSourceReference reference = new DataSourceReference();
DataSource ds = new DataSource();
dsarray = new DataSource[1];
reference.Reference = "/"+dataSourceFolder+"/"+dataSourceName;
ds.Item = reference;
ds.Name = dataSourceName;
dsarray[0] = ds;
try
{
rs.SetItemDataSources("/" + parentFolder + "/" + reportName, dsarray);
Console.WriteLine(string.Format("Set {0} DataSource to {1}",reportName,dataSourceFolder+"/"+dataSourceName));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
using System;
using System.Web.Services.Protocols;
using ReportingService.ReportService;
using System.Xml;
using System.IO;
class Sample
{
public static string ConfigFile = "Config.xml";
public static void Main()
{
ReportingService2005 rs = new ReportingService2005();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ConfigFile);
string parentFolder = xmlDoc.SelectSingleNode("//ParentFolder").InnerText;
//check whether have the parentFolder
if (rs.GetItemType("/"+parentFolder)!=ItemTypeEnum.Folder)
{
try
{
rs.CreateFolder(parentFolder, "/", null);
Console.WriteLine("Parent folder {0} created successfully", parentFolder);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
XmlNodeList nodeList = xmlDoc.SelectNodes("//ReportName");
foreach (XmlNode node in nodeList)
{
PublishReport(parentFolder, node.InnerText , rs, xmlDoc);
}
}
/// <summary>
/// Create Report and set the Data source
/// </summary>
/// <param name="parentFolder"></param>
/// <param name="reportName"></param>
/// <param name="rs"></param>
/// <param name="xmlDoc"></param>
public static void PublishReport(string parentFolder, string reportName, ReportingService2005 rs, XmlDocument xmlDoc)
{
Byte[] definition = null;
Warning[] warnings = null;
string filePath = xmlDoc.SelectSingleNode("//FilePath").InnerText;
try
{
//read report definition
FileStream stream = File.OpenRead(Path.Combine(filePath,reportName+".rdl"));
definition = new Byte[stream.Length];
stream.Read(definition, 0, (int) stream.Length);
stream.Close();
}
catch(IOException e)
{
Console.WriteLine(e.Message);
}
try
{
//create report
warnings = rs.CreateReport(reportName,"/"+parentFolder, false, definition, null);
//set report data source
SetReportDataSources(parentFolder, reportName, xmlDoc, rs);
if (warnings != null)
{
foreach (Warning warning in warnings)
{
Console.WriteLine(warning.Message);
}
}
else
{
Console.WriteLine("Report: {0} created successfully with no warnings", reportName);
}
}
catch (SoapException e)
{
Console.WriteLine(e.Detail.InnerXml.ToString());
}
}
private static void SetReportDataSources(string parentFolder,string reportName,XmlDocument xmlDoc,ReportingService2005 rs)
{
string dataSourceFolder=xmlDoc.SelectSingleNode("//DataSourceFolder").InnerText;
string dataSourceName = xmlDoc.SelectSingleNode("//DataSourceName").InnerText;
DataSource[] dsarray;
DataSourceReference reference = new DataSourceReference();
DataSource ds = new DataSource();
dsarray = new DataSource[1];
reference.Reference = "/"+dataSourceFolder+"/"+dataSourceName;
ds.Item = reference;
ds.Name = dataSourceName;
dsarray[0] = ds;
try
{
rs.SetItemDataSources("/" + parentFolder + "/" + reportName, dsarray);
Console.WriteLine(string.Format("Set {0} DataSource to {1}",reportName,dataSourceFolder+"/"+dataSourceName));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}