Referencing external .NET assemblies
You have already seen the way of adding custom code to reports and you may have already experienced the issues with it such as:
- It supports only Vb.NET
- The same function has to be added to each and every report
With this recipe, we can overcome it by making an assembly that contains all required functions and use it with the report rather repeating it in every report.
Getting ready
In order to make an assembly with all required functions, you need to create a .NET class library using one of .NET languages. Yes, it is not limited to Vb.NET. Let's make one using C#.NET language.
Follow these steps for creating a class library:
- Open the Visual Studio and create a C#.NET Class Library project. Name it as
StandardFunctions
. - Create a public class called
Functions
and add a public static method namedGetTextColor
:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StandardFunctions { public class Functions { public static string GetTextColor(string messageType) { string color = "black"; switch (messageType) { case "Warning": color = "Yellow"; break; case "Error": color = "Red"; break; case "Success": color = "Green"; break; } return color; } } }
- Open the
AssemblyInfo.cs
file in your project. AddSystem.Security
namespace and make sure theAllowPartiallyTrustedCallers
attribute is added:using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Security; [assembly: AssemblyTitle("StandardFunctions")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("StandardFunctions")] [assembly: AssemblyCopyright("Copyright © 2016")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: AllowPartiallyTrustedCallers()] [assembly: ComVisible(false)] [assembly: Guid("4d89a812-f315-48f3-af97-a7576cc4060b")] [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")]
- Get the project properties and open Application page. Make sure that the Assembly name is set as
StandardFunctions
. - Open Signing* page of Project Properties and check Sign the assembly checkbox. This is for signing the assembly with a strong name as it is required when adding to GAC. Select <New...> from Choose a strong name key file drop down:
Figure 12.09
- Set the Key file name as
StandardFunctionsKey
and uncheck Protect my key file with password. Click OK to create the file:Figure 12.10
- Save the project and build it. You should see the assembly created in the bin folder of the project:
Figure 12.11
- Now the assembly is ready. Let's see how it can be used with our reports.
How to do it...
The first thing we need to do is, make sure that the assembly can be accessed during development. This needs the assembly to be placed in the Reporting Services bin
folder and add a reference adding it with Report Properties.
Follow these steps for accessing the assembly:
- Take a copy of
StandardFunctions.dll
and place it inbin
folder of Reporting Services. The default path is C:\Program Files\Microsoft SQL Server\MSRS13.MSSQLSERVER\Reporting Services\ReportServer\bin
. - Open
Chapter12
solution and get the Properties of the DailySales report. Note that you need make sure the DailySales report is modified as per the first recipe. - Go to References page and click on Add to add a reference (see Figure 12.13). Browse to the placed
StandardFunction.dll
and select it. Click OK to add it:Figure 12.12
- Once added, you should see the assembly in the grid. Click OK to save it:
Figure 12.13
Let's use the function in the assembly with the text box we have to display the selected date:
- Go to the Text Box Properties of the text box and open Font page. Click on expression icon to set the color through an expression:
Figure 12.14
- Set the following expression. Note the way of referencing the method. It is
AssemblyName.ClassName.MethodName
. If the namespace is different, then you need to mention it as well:=Iif(format(Code.GetDate(Parameters!ReportParameterSalesDate .Value), "yyyy-MM-dd") = "1900-01-01" , StandardFunctions.Functions.GetTextColor("Error"), StandardFunctions.Functions.GetTextColor(""))
- Click on OK to save the expression and click on OK again to save the text box properties:
Note
Remember, even though we could use the assembly during development, Reporting Services requires the assembly in the Global Assembly Cache (GAC) when running the report. Let's add the assembly to GAC.
- Open Command Prompt with administrative privileges (as an Administrator) and change the path to following folder (your path may be different based on the version of Visual Studio installed):
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools
This folder has
gacutil.exe
and can be used for registering the assembly to GAC. Execute the following code to register it with GAC:gacutil.exe /i "<your folder that contains the dll>\StandardFunctions.dll"
- You should see a similar output with a success message:
Figure 12.15
Everything required is done. Let's publish the report and see whether it works:
- Go back to the project and deploy the report.
- Open the report using Web Portal. If you input an invalid date, the message set with text box should be displayed in red color:
Figure 12.16
How it works...
Remember, if the Reporting Services is installed in a different server, all you need to do is, register the assembly in GAC. If it is not registered, Reporting Services cannot access the assembly as it uses the assembly in the GAC when running the report.
The reference to the assembly is recorded in the .rdl
file. If you open the .rdl
, you will see a node that holds details of the assembly. In addition to this node, you will see an additional node if you have added Class Name
and Instance Name
with References page in Report Properties. This is not required for the code, we have written the function as a static method:
<CodeModules> <CodeModule>StandardFunctions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5021460a45a99ff0</CodeModule> </CodeModules>