Thursday, 6 October 2011

Create a New SSRS report Programmatically

This post explains about programmatically creating a new SSRS report using Reporting Services Web service.
Prerequisite : You must have SSRS(SQL Server Reporting Services) installed so that you can use Reporting web services provided by SSRS.
Follow the steps provided below:
Step 1: Add Reference to Web Service()
        The first thing you need to do is add a Web reference to the Reporting Services Web service in your development project. 
        To do so right-click on your project in Visual Studio and choose Add Service reference... .Then Click on Advanced  and then "Add Web Reference.." as shown in below image..  

Provide report service URL (in my case http://localhost/reportserver/ReportService2010.asmx). If you have a remote report server, simply change the URL of the Web reference. The end point for any Reporting Services Web service is http://servername/reportserver/ReportService2010.asmx.
Provide service reference name(i provided "ExecutionService" as reference name) and click on Add.

Step 2: Using  ReportingService.CreateCatalogItem method To create a new Report
Following code will create a SSRS report with name "NewCreatedReport" at Root of Report Server
//Create Proxy object for ReportingService
ReportingService.ReportingService2010 rs;
rs = new ReportingService.ReportingService2010();

//Set the Credentials for authentication of reportingService
rs.Credentials Net.CredentialCache.DefaultNetworkCredentials;
//Above you can also provide the credentials as below
//System.Net.NetworkCredential Mycredentials = new System.Net.NetworkCredential("yourUserName", "yourPassword", "yourDomainName");
//          rs.Credentials = Mycredentials;

rs.Url = "http://localhost/ReportServer/ReportService2010.asmx";
//_reportPath is the path where new report will get created “/” represents report will get created at root of report server. If you have some folder say “Myreports” on report server and you want to create new report inside this folder then you need to provide _reportPath=”/Myreports”
string _reportPath;
_reportPath = "/";
Byte[] reportDefinition = null;
ReportingService.Warning[] warnings = null;
ReportingService.CatalogItem newCreatedReport rs.CreateCatalogItem("Report""NewCreatedReport", _reportPath , false, reportDefinition, null, warnings);

After executing this code a new report will get created with report name/title as “NewCreatedReport” on root of reportServer i.e http://localhost/Reports

Was this helpful ????
Kindly let me know if you need more clarification.