Telerik blogs

Hi guys,

We've been having requests for the feature in the title for some time and finally we were able to introduce it in the Q3 SP2 release. 

Generally user functions allow you to extend the default behavior of the Telerik Reporting engine. They are public static  (Public Shared in VB.NET) methods that should always return a value and can take an arbitrary number of input parameters depending on your needs. They are public static, because they are not related to a concrete instance of a report. They are executed by the expression engine and work in the context of the report processing. If you have functions with one or more overload, when the function is being evaluated the expression engine would check for the appropriate one based on the function signature you provided(the name of the function, number and type of the parameters).

 Till now there were only report scoped user functions: these are public static methods of the current report class. The report scoped user functions can be invoked from an expression by their name, specifying the necessary parameters in the braces, e.g.: 

=ChangeDate(Now())

However, it is a great thing to have the ability to re-use a public static method that resides in the same assembly as the current report class, so you do not have to write the same method again for the currently used report. This way you can create whole "libraries" of user functions which are ready to use in any report in the assembly. The assembly scoped user functions can be invoked from an expression by their fully qualified name, including the full namespace and of course specifying the necessary parameters in the braces, e.g.: 

= ClassLibrary1.Report1.ChangeDate(Now())

The best part is that you do not have to manually type in the expression, but can use the "Edit Expression" dialog to select the function you need directly:

edit description dialog 

Also if the current assembly contains too many public static (Public Shared in VB.NET) methods, browsing for existing user functions might become a bit of a challenge. So in order to avoid this problem, you can use the Browsable attribute to hide any methods, which you are not going to use as user functions:

C#

public partial class Report1 : Telerik.Reporting.Report  
{  
      //...  
      [Browsable(false)]  
      public static Image ResolveUrl(string relativeUrl)  
      {  
             string path = HttpContext.Current.Server.MapPath(relativeUrl);  
             return Image.FromFile(path);  
      }  
      //...  

 

VB.NET
 Public Class Report1 Inherits Telerik.Reporting.Report  
'...  
<Browsable(False)>  
Public Shared Function ResolveUrl(ByVal relativeUrl As StringAs Image  
Dim path As String = HttpContext.Current.Server.MapPath(relativeUrl)  
Return Image.FromFile(path)  
End Function 
'...  
End Class  

 

To make the picture even more colorful, you have a Description attribute at your disposal as well. This way you can specify a clear description of what a function does.

A sample class library showing this functionality is attached.

[AssemblyScopedUserFunction.zip]


About the Author

Stefan Tsokev

Stefan’s main interests outside the .NET domain include rock music, playing the guitar and swimming.

Related Posts

Comments

Comments are disabled in preview mode.