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


  1. Thanks a ton.. i was wondering about the publishing the reports and running them. Your article gave the right path for me to look and develop. Though i haven’t excatly used th code you have put up here, it was a great help.

    Comment by Girish | July 16, 2008 | Reply

  2. Just what I was looking for! Thanks for sharing! I am going to give it a try.

    Comment by Alain | July 7, 2009 | Reply

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: