The stand-alone Telerik Report Designer tool gives you and your users the flexibility to define or edit reports visually using a codeless and easy to use interface. Sometimes however, you just can’t get around needing to extend the built-in Reporting functionality with some custom code. This blog article will walk you through creating a class library with a user defined function in Visual Studio and then use the function in the Telerik Report Designer tool.
CREATING THE CLASS LIBRARY
In order to use custom code in Telerik Report Designer, you must first create the custom code. In order to accomplish this, create a new class library project in Visual Studio. In this case, I’ve created a new C# class library project and named it “ReportLibrary”. I’ve also deleted the Class1.cs that is generated when the project is created.
To set the stage for this example, the Adventure Works Company requires a report that will identify the shipping method of products purchased from its online store. If a product weighs less than 5 pounds, the traditional first class postage will be utilized. Any product between 5 and 20 pounds will utilize a private logistical company, and anything over 20 pounds will be required to be picked up by the customer at the warehouse.
First we will need to add a reference to the Telerik.Reporting assembly. Then add a new class to the class library project, in this case I’ve named the class “PostageFunctions.cs”. Replace the code in this source file with the following:
public static class PostageFunctions
Description="Identifies the type of postage based on product weight (lbs)")]
public static string GetPostageTypeByWeight(decimal itemWeight,
itemWeight = GramsToPoundsConverter(itemWeight);
string postageType = "Priority Mail";
if (itemWeight > 5)
postageType = "Logistical Service";
if (itemWeight >= 20)
postageType = "Pick Up Only";
private static decimal GramsToPoundsConverter(decimal weightInGrams)
//1lb = 453.59237g
decimal poundInGrams = Convert.ToDecimal(453.59237);
return weightInGrams / poundInGrams;
Important things to note in this code listing is that the class and methods that you wish to expose to the Report Designer must be static. The attributes added to the GetPostageTypeByWeight provides usability hints for the Report Designer to pick up and display to your users so they know how to use the custom code that you provided to them.
Build the class library and copy the resulting DLL into the same folder as your Telerik Report Designer executable, if you’ve used the default installation path, it should be located in a folder similar to the following:
C:\Program Files (x86)\Telerik\Reporting Q2 2013\Report Designer
INCLUDING THE LIBRARY IN THE REPORT DESIGNER
Now that the DLL containing the custom code is located in the same folder as the Report Designer executable, we will need to modify the config file in order for the tool to pick it up. In the same directory as the executable, open the ReportDesigner.exe.config file and add the following entry to the assemblies section:
<!-- Add assembly references -->
<add name="MyFunctions" version="18.104.22.168" />
<add name="ReportLibrary" version="22.214.171.124"
culture="neutral" publicKeyToken="null" />
USING THE LIBRARY
Now that the assembly is referenced in the configuration file, you will now be able to use it in a report. Create a new Report in report designer, exit out of the wizard, and delete the header and footer sections of the report, leaving only the details section. Add a SQL Data Source component and use the Adventure Works database with the following query:
SELECT TOP 200 [ProductID]
WHERE WEIGHT IS NOT NULL
Next drag and drop an instance of the Table Wizard to the design surface, using the fields Name, ProductNumber, StandardCost, Weight and Weight Unit of Measure as the columns from the SQL Data Source. Next right click on the table, and add a column. Add a textblock label and setting its value to Postage Type. Then add a texblock to display the value directly beneath the title label. We will set this value to an expression. Once we load the expression builder, you will now see that our PostageFunctions are available for us to use, we will then pass in the field values of weight and weight unit of measure code.
Now when we preview the report, we will see that our custom postage type function is being used and displayed in the table.
The stand-alone Telerik Report Designer gives users lots of freedom to create and edit reports without requiring them to learn how to code. In this blog post we demonstrated that you can still provide your users with libraries of custom functions to extend the built-in capabilities of the tool and still shield them from the curly braces and semi-colons of code.