Monday, 30 December 2013

Passing Parameter values from code (C#) to SSRS

Technology:

Microsoft Visual Studio 2010 with C# & ASP.NET,  Framework 4.0
Microsoft SSRS 2005
Microsoft Sharepoint 2012

At some point, you may encounter a scenario whereby you need to pass a default value from an ASP.Net application (using C#) to an SSRS report parameter hosted on Sharepoint. For example, an application may have a 'View Report' button which takes the user to an external SSRS report hosted in Sharepoint. If the user is working with a unique set of data in their application, the report should ideally be filtered for this set of data also.

In Sharepoint, a report's URL will be:

http://<SharePoint_site>/_layouts/ReportServer/RSViewerPage.aspx?rv:RelativeReportUrl=/<SharePoint_Document_Library>/<Report_Name>.rdl




You can specify a report parameter name and value pair by explicitly specifying the prefix "rp:" (Note that the report parameter name must match the parameter name that is specified in the SSRS report). Here is an example to add-on a parameter with name "Month" that accepts a string value:
 
http://<SharePoint_site>/_layouts/ReportServer/RSViewerPage.aspx?rv:RelativeReportUrl=/<SharePoint_Document_Library>/<Report_Name>.rdl&rp:Month=January

You can specify multiple report parameter name and value pairs by explicitly specifying the prefix "rp:" separated by an '&' symbol: 
http://<SharePoint_site>/_layouts/ReportServer/RSViewerPage.aspx?rv:RelativeReportUrl=/<SharePoint_Document_Library>/<Report_Name>.rdl&rp:Month=January&rp:Year=2014
Multiple parameter name and value pairs can be concatenated together in a separate helper method utilising the StringBuilder class. This method can then return the full parameter string which can be appended to the main report URL in order to produce the full URL.
 
When the user clicks the 'View Report' button in the application, we can use the following statement to open the parameterised report in its report library:

ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "Open", String.Format("window.open('{0}');", <reportUrl>), true);



No comments:

Post a Comment