Wednesday, March 28, 2012

Running a Report deployed on Reporting Server from C#

I have a report deployed on reporting serice. I am using reporting web service.

How can we call the report, assign parameters and execute it on a webpage

Are you using the full-blown version of Visual Studio 2005? If so, use a report viewer.

I'm not sure what a "reporting web service" is.

|||ReportExecution2005.asmx|||

Is that a link? If so it isn't clickable.

|||

http://msdn2.microsoft.com/en-us/library/microsoft.wssux.reportingserviceswebservice.rsexecutionservice2005.aspx

I was refereing to the Reporting Services Web Service

Its ReportExecutionService

|||

Ok. Either I'm very mistaken or you are quite confused. That is the service which allows reports to render and generate.

I don't think there is any claim in this document that is made that would indicate that you can use this service directly to view reports from a webpage.

What I was getting at is you most likely want to create a .net web application and use the report viewer within Visual Studio 2005.

|||

Yeah i was gonna do that approach, get the bytes from webserivces render and using response to write to the page

Do u know how we can use the reportviwer to use the bytes returned from the webserivce

|||

Yes. In a nutshell, you do the following:

1. Create a C# .Net web application.

2. Drag and drop a report viewer to the application.

3. Set the report viewer properties properly to point to the report.

|||

Here's code I used from my application to generate a report, and get the byte stream. You'll note code in there that is used by HTML reports to point to the location of the images returned from the report service (all of which need to be saved to disk in my case). However, you can probably do all of this via stream directly to the output stream as well. Ignore my application-specific code:

Code Snippet

// load the report meta data and execution data

ReportExecutionService res = new ReportExecutionService();

res.Credentials = System.Net.CredentialCache.DefaultCredentials;

res.Url = application.ReportServer + "ReportExecution2005.asmx";

ReportingService2005 rs = new ReportingService2005();

rs.Credentials = System.Net.CredentialCache.DefaultCredentials;

ReportService2005.DataSourceCredentials[] credentials = null;

ReportService2005.ReportParameter[] availableParameters = rs.GetReportParameters(reportPath, null, false, null, credentials);

string outputID = IdGenerator.GetRandomStringId(20, null, null, true, true);

string outputFileName = outputID + "." + ext;

bool gecko = false;

if (request != null && request.UserAgent != null && request.UserAgent.ToLower().Contains("firefox"))

{

gecko = true;

}

string deviceInfo = "/" + site.RewriteUrl(application.VirtualPathRoot + "/Content/Resource.aspx?type=tmp") + "&filename=" + outputID + "_" + (gecko ? "Gecko" : "") + "";

deviceInfo = deviceInfo.Replace("&s=" + site.Key, "&s=" + site.Key);

string historyID = null;

string encoding = null;

string mimeType = null;

string extension = null;

ReportExecution2005.Warning[] warnings = null;

string[] streamIDs = null;

ExecutionInfo execInfo = new ExecutionInfo();

ExecutionHeader execHeader = new ExecutionHeader();

res.ExecutionHeaderValue = execHeader;

execInfo = res.LoadReport(reportPath, historyID);

string sessionId = res.ExecutionHeaderValue.ExecutionID;

// add all execution parameters

PrepareReportServerExecutionParameters(res, reportParameterCollection, format, parameters, availableParameters, application, site);

// execute the report to a file

byte[] result = res.Render(format, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);

FileStream stream = null;

try

{

stream = File.Create(application.TempFolder + outputFileName, result.Length);

stream.Write(result, 0, result.Length);

}

finally

{

if (stream != null)

{

stream.Close();

}

}

if (streamIDs != null && streamIDs.Length > 0)

{

for (int i = 0; i < streamIDs.Length; i++)

{

string encodingImage;

string mimeTypeImage;

System.IO.FileStream fs = null;

try

{

byte[] image = res.RenderStream("HTML4.0", streamIDs[i], null, out encodingImage, out mimeTypeImage);

fs = System.IO.File.OpenWrite(application.TempFolder + outputID + "_" + streamIDs[i]);

fs.Write(image, 0, Convert.ToInt32(image.Length));

}

catch (Exception e)

{

Log.Error(site, "Error rendering image stream for report (" + reportParameterCollection.ReportToken + "). " + e.Message + " " + e.StackTrace);

}

finally

{

if (fs != null)

{

fs.Close();

}

}

}

}

The "PrepareReportServerExecutionParameters()" method adds parameters to the report as follows:

Code Snippet

ReportExecution2005.ParameterValue parameter = new ReportExecution2005.ParameterValue();

parameter.Name = name;

parameter.Value = value;

reportServerParameterValues.Add(parameter);

The "reportServerParameterValues" collection is eventually funnelled into this call:

Code Snippet

res.SetExecutionParameters(reportParameters, "en-us");

I hope that all helps!

Michael

No comments:

Post a Comment