Get SSRS report programatically using C#/.NET

To get the SSRs report programmatically, ReportExecutionService reference is all we need.

Browse http://Server/ReportIISDirectory/ReportExecution2005.asmx, this is the path of the report execution service, that provide us functionality to pull the SSRS report directly from server in any of the format (XML, Excel etc.)

Create a web reference to the service or use WSDL.exe to create a proxy class.
Then,

ReportExecutionService re = new ReportExecutionService();
re.Url = "http:///ReportIISDirectory/ReportExecution2005.asmx";

string historyID = null;
string deviceInfo = null;
string format = "XML"; //To get the report in XML format
Byte[] results;
string encoding = String.Empty;
string mimeType = String.Empty;
string extension = String.Empty;
Warning[] warnings = null;
string[] streamIDs = null;

//path of the report
string _reportName = "/Report1"

// Set the default credentials for the server report
re.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

//Load the report
re.LoadReport(_reportName, historyID);

//set the data source credentials if required, else comment this piece of code
DataSourceCredentials[] _credentials = new DataSourceCredentials[1];
_credentials[0] = new DataSourceCredentials();
_credentials[0].DataSourceName = "DataSourceName";
_credentials[0].UserName = "UserName";
_credentials[0].Password = "Password";
re.SetExecutionCredentials(_credentials);

//Set the parameters if required
//change the length of array as per your requirement
ParameterValue[] parameters = new ParameterValue[1];

parameters[0] = new ParameterValue()
{
    Name = "Param1",
    Value = "Value"
};

re.SetExecutionParameters(parameters, "en-us");

//get the report from server in XML format
results = re.Render(format, deviceInfo,
          out extension, out encoding,
          out mimeType, out warnings, out streamIDs);

//load the report in XMLDocument
XmlDocument xDoc = new XmlDocument();

using (Stream stream = new MemoryStream(results))
{
    xDoc.Load(stream);
}

//Now use this XML document to travese through the SSRs report

This we are using to write the unit test for SSRS report using C#.

We have created a helper to get the SSRS report in form of XML, and customise the helper to accept any number of parameters and other fields as well.

hope this will be usefull.

Tags: , , ,
Subscribe to Comments RSS Feed in this post

2 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*