How to use an external .NET assembly in report layout for RTC reports

How to use an external .NET assembly in report layout for RTC reports

  • Comments 3

This post shows how to include an external .NET assembly in your report layout when designing reports for RTC. The focus here is NOT how to build a .NET assembly, but how you can include such an assembly in your report design. But still, we will begin by creating our own .NET assembly for use in the layout.

 

Creating a .NET assembly

To keep it simple, let's make a .NET assembly to just add up two numbers:

  1. Start Visual Studio.
  2. Create a new c# project of type Class Library, and name it MyReportAssembly.
  3. Change the default Class name from Class1 to AddNumbers.
  4. Create a public function called AddFunction, which takes two integers as parameters, and returns the sum of those two.

The whole project should look like this now:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyReportAssembly
{
    public class AddNumbers
    {
        public int AddFunction(int i, int j) {
            return (i + j);
        }
    }
}

That's all the functionality we need - as mentioned we want to keep it simple!

But we need to make a few more changes to the project before we can use it:

 

Add a strong named key

In Project Properties, on the Signing tab, select "Sign the assembly", select a New key (give it any name), and untick password protection.

 

Add AllowPartiallyTrustedCallers property

To allow this assembly to be called from the report layout you must set this property in Assemblyinfo.cs. So, in Assemblyinfo.cs, add these lines:

using System.Security;

[assembly: AllowPartiallyTrustedCallers]

Full details of why you need those two lines here: "Asserting Permissions in Custom Assemblies"

 

Compile to .NET 3.5

When I built this project using Visual Studio 2010, I was not able to install the assembly. And when trying to include it in my report layout I got this error: "MyReportAssembly.dll does not contain an assembly.". So if you will be using the installation instructions below, and you are using Visual Studio 2010, then change "Target Framework" to ".NET Framework 3.5" under Project Properties on the Application tab. This is the default target framework in Visual Studio 2008. Visual Studio 2010 defaults to version 4.0. I'm sure there are better ways to instlal this and still build it for .NET 4, but that's outside of the current scope. Also if it later complains about reference to Microsoft.CSharp, then just remove that from the project under references.

 

When this is done, build your project to create MyReportAssembly.dll

 

Installing your new .NET assembly

Again, the focus here is not on buildign and installing .NET assemblies, and I am no expert in that, so this is probably NOT the recommended way to install a .NET assembly, but it works just for the purpose of being able to see it in the report layout:

Start an elevated Visual Studio Command prompt, and go to the folder where your net MyReportAssembly.dll is (C:\Users\[UserName]\Documents\visual studio 2010\Projects\MyReportAssembly\MyReportAssembly\bin\Debug\). Then run this command:


gacutil /i MyReportAssembly.dll

It can be uninstalled again with this command:


gacutil /uf MyReportAssembly

After installing it, open this folder in Windows Explorer:


c:\Windows\Assembly


and check that you have the MyReportAssembly there. If not, then check if the section above about compiling it to .NET 3.5 applies to you.

 

Finally - How to use an exterenal .NET assembly in report layout
 

So now we finally come to the point of this post: How do you use your new assembly in the report layout:

  1. Design any report, and go to the report Layout.
  2. Create a reference to your assembly: Report -> Report Properties -> References. Browse for your new .dll and select it.
  3. On the Code tab, create a function which will create an instance of your assembly and call the function:

 Public Function Addnumbers(Num1 as Integer, Num2 as Integer) as Integer
  Dim MyAssembly as MyReportAssembly.AddNumbers
  MyAssembly = new MyReportAssembly.AddNumbers()
  Return MyAssembly.AddFunction(Num1,Num2)
End Function

 Then call this function nby adding this Expression to a TextBox:

=Code.Addnumbers(1,2)

And, finally, if you run the report like this, you would get the error "xyz, which is not a trusted assembly.". So back in the classic report design, in report properties, you just have to set the property EnableExternalAssemblies = Yes, and the report should run.

 

That was a lot of work for just adding up two numbers, but hopefully is shows what steps are needed to open up your report layout to endless opportunities. Note: I have no ideas if this will work with visual assemblies or anything which contains any UI at all. Any experiences with this, feel free to add to the end of this post.

 

As always, and especially this time since I'm in no way a c# developer:

These postings are provided "AS IS" with no warranties and confer no rights. You assume all risk for your use.

 

Additional Information

If you plan to look further into this, then here are some recommended links:

"Using Custom Assemblies with Reports"

"Deploying a Custom Assembly"

If your assembly calls other services, it is likely you need to consider passing on the user's credentials. For more information on that, here is a good place to begin:

"Asserting Permissions in Custom Assemblies"

 

 

Lars Lohndorf-Larsen

Microsoft Dynamics UK

Microsoft Customer Service and Support (CSS) EMEA

 

Leave a Comment
  • Please add 1 and 6 and type the answer here:
  • Post
  • Hi Lars, great effort!thanks for sharing. I'm surely gonna try it in my free time :)

    I have a question: can this be extended to javascript and/or AJAX?

  • Thanks for your comments!

    You can use any .NET compatible language yes, as long as you finish building it as a valid .NET assembly. However, I'm not able to give you any samples in javascript or AJAX, unfortunately.

  • Yup, that's enough!otherwise you would steal all the entertainment to us :), thanks a lot!

Page 1 of 1 (3 items)