Skip to content

Instantly share code, notes, and snippets.

@paulodiogo
Last active July 21, 2020 20:06
Show Gist options
  • Select an option

  • Save paulodiogo/ca9f9af2d309f3ce07ae74baa3d3b79d to your computer and use it in GitHub Desktop.

Select an option

Save paulodiogo/ca9f9af2d309f3ce07ae74baa3d3b79d to your computer and use it in GitHub Desktop.
Using SSRS 2016
public class Report
{
public Report(string extension, string mimeType, byte[] results)
{
this.Extension = extension;
this.MimeType = mimeType;
this.Results = results;
}
public string Extension { get; private set; }
public string MimeType { get; private set; }
public byte[] Results { get; private set; }
}
//Project > References > Add Service Reference > http://<URL>/<APP_SSRS>/ReportExecution2005.asmx
//Add the SOAP Execution client
public class ExecuteReport
{
private const string reportName = @"<path>/name";
private readonly SSRSEX.ReportExecutionServiceSoapClient clientEX;
//NetworkCredential => CredentialCache.DefaultNetworkCredentials or new System.Net.NetworkCredential("user", "pass", "domain")
public ExecuteReport(System.Net.NetworkCredential clientCredentials)
{
clientEX = new SSRSEX.ReportExecutionServiceSoapClient();
clientEX.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
clientEX.ClientCredentials.Windows.ClientCredential = clientCredentials;
}
public byte[] Render(string formato, out string mimeType, out string extension)
{
string historyID = null;
string deviceInfo = null;
byte[] results;
string encoding = string.Empty;
SSRSEX.Warning[] warnings = null;
string[] streamIDs = null;
clientEX.Open();
SSRSEX.ServerInfoHeader serverInfoHeader = null;
SSRSEX.ExecutionInfo executionInfo = null;
SSRSEX.TrustedUserHeader trustedUserHeader = new SSRSEX.TrustedUserHeader();
clientEX.LoadReport(trustedUserHeader, reportName, historyID, out serverInfoHeader, out executionInfo);
SSRSEX.ParameterValue[] parameters = new SSRSEX.ParameterValue[1];
parameters[0] = new SSRSEX.ParameterValue();
parameters[0].Name = "ParameterName";
parameters[0].Value = "ParameterValue";
SSRSEX.ExecutionHeader execHeader = new SSRSEX.ExecutionHeader();
execHeader.ExecutionID = executionInfo.ExecutionID;
clientEX.SetExecutionParameters(execHeader, trustedUserHeader, parameters, "pt-BR", out executionInfo);
clientEX.Render(execHeader, trustedUserHeader, formato, deviceInfo,
out results, out extension, out mimeType, out encoding, out warnings, out streamIDs);
return results;
}
public async System.Threading.Tasks.Task<Report> Render(string formato)
{
string historyID = null;
string deviceInfo = null;
string encoding = string.Empty;
clientEX.Open();
SSRSEX.ServerInfoHeader serverInfoHeader = null;
SSRSEX.ExecutionInfo executionInfo = null;
SSRSEX.TrustedUserHeader trustedUserHeader = new SSRSEX.TrustedUserHeader();
clientEX.LoadReport(trustedUserHeader, reportName, historyID, out serverInfoHeader, out executionInfo);
SSRSEX.ParameterValue[] parameters = new SSRSEX.ParameterValue[1];
parameters[0] = new SSRSEX.ParameterValue();
parameters[0].Name = "ParameterName";
parameters[0].Value = "ParameterValue";
SSRSEX.ExecutionHeader execHeader = new SSRSEX.ExecutionHeader();
execHeader.ExecutionID = executionInfo.ExecutionID;
clientEX.SetExecutionParameters(execHeader, trustedUserHeader, parameters, "pt-BR", out executionInfo);
SSRSEX.RenderRequest renderReq = new SSRSEX.RenderRequest(execHeader, trustedUserHeader, formato, deviceInfo);
SSRSEX.RenderResponse taskRender = await clientEX.RenderAsync(renderReq);
return new Report(taskRender.Extension, taskRender.MimeType, taskRender.Result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment