Add Filter to Reports

1 Answer 15 Views
Report Parameters
Ken
Top achievements
Rank 1
Ken asked on 09 Mar 2025, 05:02 PM

I have been following the tutorials on creating PDFs of existing reports.  I have a function where I pass in the path name to the report (TRDP file).

Can you add filter or parameter to the report?

 

            ReportProcessor reportProcessor = new ReportProcessor();
            Telerik.Reporting.UriReportSource uriReportSource = new Telerik.Reporting.UriReportSource();
            uriReportSource.Uri = reportPathName;

            //uriReportSource.Parameters.Add();

            Console.WriteLine("Rendering Report " + Path.GetFileName(reportPathName));
            RenderingResult result = reportProcessor.RenderReport("PDF", uriReportSource, null);
            Console.WriteLine("Done Rendering Report " + Path.GetFileName(reportPathName));

            bool success = !result.HasErrors;

1 Answer, 1 is accepted

Sort by
0
Petar
Telerik team
answered on 11 Mar 2025, 04:56 PM

Hello Ken,

Thank you for providing the code snippet.

I believe I answered your question in the private ticket 1681194. However, I will repeat my answer here, as it may also be helpful for other users in our Q&A forum. If you need further assistance with this issue, I recommend using the private ticket instead, as the response time there is 24 hours.

To add a filter to the report before rendering it, you should first unpackage the report file. This process allows you to modify the report programmatically by interacting with the Telerik.Reporting.Report instance. Once you have made your modifications, such as adding a filter to the report or any of its data items, you can use an InstanceReportSource instance (instead of the UriReportSource currently in use) with the modified report instance. Then, pass this instance to the ReportProcessor.RenderReport method to render the report:

using Telerik.Reporting.Processing;

// ...

Telerik.Reporting.Processing.ReportProcessor reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();

Telerik.Reporting.ReportPackager reportPackager = new Telerik.Reporting.ReportPackager();
using (var sourceStream = System.IO.File.OpenRead(reportPathName))
{
    // unpackage the report
    Telerik.Reporting.Report report = (Telerik.Reporting.Report)reportPackager.UnpackageDocument(sourceStream);

    // use an InstanceReportSource instead of UriReportSource
    Telerik.Reporting.InstanceReportSource instanceReportSource = new Telerik.Reporting.InstanceReportSource();

    // add the filter
    Telerik.Reporting.Filter filter = new Telerik.Reporting.Filter();
    filter.Expression = "=Fields.ProductID";
    filter.Operator = Telerik.Reporting.FilterOperator.GreaterThan;
    filter.Value = "=10";
    report.Filters.Add(filter);

    instanceReportSource.ReportDocument = report;

    Console.WriteLine("Rendering Report " + Path.GetFileName(reportPathName));
    // pass the InstanceReportSource instance with the modified report to the RenderReport method of the ReportProcessor
    RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, null);
    Console.WriteLine("Done Rendering Report " + Path.GetFileName(reportPathName));

    bool success = !result.HasErrors;

    if (success == true)
    {
        //string fileName = result.DocumentName + "." + result.Extension;
        string fileName = Path.GetFileNameWithoutExtension(reportPathName) + "." + result.Extension;
        string path = AppContext.BaseDirectory; //   System.IO.Path.GetTempPath();
        string filePath = System.IO.Path.Combine(path, fileName);

        using (System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
        {
            fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
        }
        success = true;
    }
    else
    {
        Console.WriteLine(result.Errors);
    }
}

The process of adding filters programmatically is shown in the following documentation article:

As for how to add a parameter to the report, you should be able to achieve this through the Parameters collection property of the InstanceReportSource before rendering it:

    instanceReportSource.Parameters.Add("Parameter1", "234");

Alternatively, if you want to stick to the UriReportSource approach (note that you won't be able to pass the modified report instance with the filter in this case), you can use its Parameters property as well, similarly to the above approach. Note that in order for this to work, you need to create the parameter during design time with a design-time value being said (for example, in the report designer), or an exception will be thrown.

If you want to avoid this limitation, you can set the parameter directly to the report programmatically (similar to how the filter is applied in the above code snippet). This would act the same as setting it during design time and won't cause an exception if not set beforehand:

    Telerik.Reporting.ReportParameter reportParameter = new Telerik.Reporting.ReportParameter();  
    reportParameter.Name = "Parameter1";  
    reportParameter.Type = Telerik.Reporting.ReportParameterType.String;  
    reportParameter.Value = "234";  
  
    report.ReportParameters.Add(reportParameter);

I hope this helps. Just as a reminder, if you need further assistance with this issue, I recommend using the private ticket instead, as the response time there is 24 hours.

Regards,
Petar
Progress Telerik

Enjoyed our products? Share your experience on G2 and receive a $25 Amazon gift card for a limited time!

Ken
Top achievements
Rank 1
commented on 15 Mar 2025, 02:10 PM

That is what I was looking for.  Thank you.
Tags
Report Parameters
Asked by
Ken
Top achievements
Rank 1
Answers by
Petar
Telerik team
Share this question
or