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.

Revisions

  1. diogo.leao revised this gist Jun 8, 2017. 3 changed files with 61 additions and 26 deletions.
    9 changes: 9 additions & 0 deletions Format.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    [Serializable]
    public enum Format
    {
    XML,
    CSV,
    PDF,
    EXCELOPENXML,
    WORDOPENXML
    }
    51 changes: 34 additions & 17 deletions SSRS.cs
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,25 @@
    //Project > References > Add Service Reference > http://<URL>/<APP_SSRS>/ReportExecution2005.asmx
    //Add the SOAP Execution client

    public class ExecuteReport
    public class GenerateReportService
    {

    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)
    public GenerateReportService(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)
    public Report Render(string reportName, IDictionary<string, string> parametros, Format format)
    {
    string mimeType = string.Empty;
    string extension = string.Empty;
    string historyID = null;
    string deviceInfo = null;
    byte[] results;
    @@ -34,23 +35,31 @@ public byte[] Render(string formato, out string mimeType, out string extension)

    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";

    List<ParameterValue> reportParameters = new List<ParameterValue>();

    foreach (var item in parameters)
    {
    reportParameters.Add(new ParameterValue
    {
    Name = item.Key,
    Value = item.Value
    });
    }

    SSRSEX.ExecutionHeader execHeader = new SSRSEX.ExecutionHeader();
    execHeader.ExecutionID = executionInfo.ExecutionID;

    clientEX.SetExecutionParameters(execHeader, trustedUserHeader, parameters, "pt-BR", out executionInfo);
    clientEX.SetExecutionParameters(execHeader, trustedUserHeader, reportParameters.ToArray(), "pt-BR", out executionInfo);

    clientEX.Render(execHeader, trustedUserHeader, formato, deviceInfo,
    clientEX.Render(execHeader, trustedUserHeader, format.ToString(), deviceInfo,
    out results, out extension, out mimeType, out encoding, out warnings, out streamIDs);

    return results;
    return new Report(extension, mimeType, results);

    }

    public async System.Threading.Tasks.Task<Report> RenderAsync(string formato)
    public async System.Threading.Tasks.Task<Report> RenderAsync(string reportName, IDictionary<string, string> parameters, Format format)
    {
    string historyID = null;
    string deviceInfo = null;
    @@ -64,16 +73,24 @@ public async System.Threading.Tasks.Task<Report> RenderAsync(string formato)

    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";

    List<ParameterValue> reportParameters = new List<ParameterValue>();

    foreach (var item in parameters)
    {
    reportParameters.Add(new ParameterValue
    {
    Name = item.Key,
    Value = item.Value
    });
    }

    SSRSEX.ExecutionHeader execHeader = new SSRSEX.ExecutionHeader();
    execHeader.ExecutionID = executionInfo.ExecutionID;

    clientEX.SetExecutionParameters(execHeader, trustedUserHeader, parameters, "pt-BR", out executionInfo);
    clientEX.SetExecutionParameters(execHeader, trustedUserHeader, reportParameters.ToArray(), "pt-BR", out executionInfo);

    SSRSEX.RenderRequest renderReq = new SSRSEX.RenderRequest(execHeader, trustedUserHeader, formato, deviceInfo);
    SSRSEX.RenderRequest renderReq = new SSRSEX.RenderRequest(execHeader, trustedUserHeader, format.ToString(), deviceInfo);
    SSRSEX.RenderResponse taskRender = await clientEX.RenderAsync(renderReq);

    return new Report(taskRender.Extension, taskRender.MimeType, taskRender.Result);
    27 changes: 18 additions & 9 deletions UsingController.cs
    Original file line number Diff line number Diff line change
    @@ -1,28 +1,37 @@
    public class UsingController
    public class UsingController
    {

    private readonly ExecuteReport executer;
    private readonly GenerateReportService executer;

    public UsingController(ExecuteReport executer)
    public UsingController(GenerateReportService executer)
    {
    this.executer = executer;
    }

    public async System.Threading.Tasks.Task<ActionResult> IndexOne(string format)
    public async System.Threading.Tasks.Task<ActionResult> IndexOne(int year, string format)
    {
    var relatorio = await this.executer.RenderAsync(format);
    var parameters = new Dictionary<string, string>();

    parameters.Add("Year", year.ToString());

    var reportFormat = (Format)Enum.Parse(typeof(Format), format);

    var relatorio = await this.executer.RenderAsync("reportName", parameters, format);

    return File(relatorio.Results, relatorio.MimeType, string.Format("relatorio.{0}", relatorio.Extension));
    }

    public ActionResult IndexTwo(string format)
    {
    string mimeType = null;
    string extension = null;
    var parameters = new Dictionary<string, string>();

    var bytes = this.executer.Render(format, out mimeType, out extension);
    parameters.Add("Year", year.ToString());

    return File(bytes, mimeType, string.Format("relatorio.{0}", extension));
    var reportFormat = (Format)Enum.Parse(typeof(Format), format);

    var relatorio = this.executer.Render("reportName", parameters, format);

    return File(relatorio.Results, relatorio.MimeType, string.Format("relatorio.{0}", relatorio.Extension));
    }


  2. paulodiogo revised this gist May 31, 2017. No changes.
  3. paulodiogo revised this gist May 31, 2017. No changes.
  4. paulodiogo revised this gist May 31, 2017. No changes.
  5. paulodiogo revised this gist May 31, 2017. 1 changed file with 29 additions and 0 deletions.
    29 changes: 29 additions & 0 deletions UsingController.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    public class UsingController
    {

    private readonly ExecuteReport executer;

    public UsingController(ExecuteReport executer)
    {
    this.executer = executer;
    }

    public async System.Threading.Tasks.Task<ActionResult> IndexOne(string format)
    {
    var relatorio = await this.executer.RenderAsync(format);

    return File(relatorio.Results, relatorio.MimeType, string.Format("relatorio.{0}", relatorio.Extension));
    }

    public ActionResult IndexTwo(string format)
    {
    string mimeType = null;
    string extension = null;

    var bytes = this.executer.Render(format, out mimeType, out extension);

    return File(bytes, mimeType, string.Format("relatorio.{0}", extension));
    }


    }
  6. paulodiogo revised this gist May 31, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion SSRS.cs
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,7 @@ public byte[] Render(string formato, out string mimeType, out string extension)

    }

    public async System.Threading.Tasks.Task<Report> Render(string formato)
    public async System.Threading.Tasks.Task<Report> RenderAsync(string formato)
    {
    string historyID = null;
    string deviceInfo = null;
  7. paulodiogo revised this gist May 31, 2017. No changes.
  8. paulodiogo revised this gist May 31, 2017. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions Report.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    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; }
    }
  9. paulodiogo created this gist May 31, 2017.
    83 changes: 83 additions & 0 deletions SSRS.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    //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);

    }

    }