Running on Empty

The few things I know, I like to share.

Programmatically rendering SRS reports, published on SharePoint, using C#.

Step 1:  Create Web Reference

Step 2:  Implement the Report Execution Service in Code.

private static ReportExecutionService reportExecutionService = new ReportExecutionService();

Step3: Implement Report Create Methods.

///
/// Create a report with a reportname, parameter array and a filename.
///
///
///
///
public static void CreateReport(ParameterValue[] parameterArray, string reportName, string fileName)
{
    CreateReport(parameterArray, reportName, fileName, ReportFormat.PDF);
}

///
/// Create a report with a reportname, parameter array, filename and a report format
///
///
///
///
///
public static void CreateReport(ParameterValue[] parameterArray, string reportName, string fileName, ReportFormat format)
{
    reportExecutionService.Url = EnvSettings.Instance.ReportSettings.Url;
    reportExecutionService.Credentials = ServiceFactory.ServiceCredentials;
    reportUrl = string.Format("{0}{1}.rdl", EnvSettings.Instance.ReportSettings.ReportRepository, reportName);

    Byte[] results;
    string deviceinfo = null;
    string extension = String.Empty;
    string encoding = String.Empty;
    string mimeType = String.Empty;
    Warning[] warnings = null;
    string[] streamIds = null;
    string historyId = null;

    ExecutionInfo ei = reportExecutionService.LoadReport(reportUrl, historyId);

    reportExecutionService.SetExecutionParameters(parameterArray, "en-us");
    results = reportExecutionService.Render(format.ToString(), deviceinfo, out extension, out encoding, out mimeType, out warnings, out streamIds);
    SaveReport(results, fileName);
}
/// <summary>
/// Saves the report to a file, accepts a byte array of the report
/// </summary>
/// <param name="results"></param>
/// <param name="fileName"></param>
private static void SaveReport(Byte[] results, string fileName)
{
    using (FileStream stream = File.OpenWrite(fileName))
    {
        stream.Write(results, 0, results.Length);
    }
}

Step 4: Use Report Creation in Code.

public static void GenerateInvoiceReport(CommercialInvoice invoice, string fileName)
{
    // Generate invoice
    ParameterValue[] parameters = new ParameterValue[1];
    parameters[0] = new ParameterValue();
    parameters[0].Label = "CommercialInvoiceId";
    parameters[0].Name = "CommercialInvoiceId";
    parameters[0].Value = invoice.Id.Value.ToString();
    ReportPrinterBC.CreateReport(parameters, "CommercialInvoice", fileName);

    //Generate invoice transaction details
    string ext = Path.GetExtension(fileName);
    fileName = Path.GetFileNameWithoutExtension(fileName);
    ParameterValue[] transactionDetailParameters = new ParameterValue[1];
    transactionDetailParameters[0] = new ParameterValue();
    transactionDetailParameters[0].Label = "CommercialInvoiceId";
    transactionDetailParameters[0].Name = "CommercialInvoiceId";
    transactionDetailParameters[0].Value = invoice.Id.Value.ToString();
    ReportPrinterBC.CreateReport(parameters, "CommercialInvoiceTransactionDetails", string.Format("{0}_TransDetail{1}", fileName, ext));
}

December 19, 2007 Posted by | C#, SharePoint, SRS | 2 Comments

   

Follow

Get every new post delivered to your Inbox.