The K2 Pirate

March 26, 2011

Custom SSRS Code in external assembly (Resolve AD usernames to displaynames for reports)

Filed under: K2 Blackpearl,SSRS Reporting — Tags: , , — hoshy @ 10:24 pm

A common requirement in reports is to display the AD display name of a user rather than their username.  Ashley Evans, rather than DOMAIN\ashley for example.

There are of course a few ways to do it, but sometimes you just need to get the display name in the report and not worry about the rest of it.

This is where the attached code comes in to play.  It's a .net assembly that simply resolves a username to a display name, what's special about it is that it's strongly named, and includes the required code access security configuration to allow it to be used from within Reporting Services.

You can use it as it is, or just as an example of how to call custom code from SSRS.

There's one static method that takes a username in one of the following formats, and returns the display name.  If it fails for any reason it simply returns the input data allowing at least something sensible to be displayed in the report.  It's also multi-domain aware.

Input formats:

  • SecurityLabel:DOMAIN\Username (e.g. K2:DOMAIN\Username)
  • SecurityLabel:Username
  • DOMAIN\Username
  • Username

To use it you'll need to GAC the assembly in the attached project and deploy the CAS configuration to both the rssvrpolicy.config and/or rspreviewpolicy.config files.  The former allows the assembly permission to be loaded from SSRS for running the reports and the latter is for previewing the reports in Visual Studio 2008.

The default locations of the config files are:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\rspreviewpolicy.config
C:\Program Files\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Reporting Services\ReportServer\bin\rssvrpolicy.config

The required CAS configuration for the policy config files is in the attached project in a file called: report server.config

It should be placed just above the final two closing </CodeGroup> tags.

To use the code.  Add a reference in your Visual Studio report project as follows:

Then in the field where you want to do the lookup, configure an expression similar to this:

=SSRSADInterop.ADLookup.UserDisplayName(Fields!Originator.Value)

Hey presto.  It works.

If you get errors, check the CAS configuration – it can be a bit fiddly to get working.  Ok, that's an understatement – it took me bloody ages the first time!

Have fun,

Hoshy, the K2 Pirate ;-)

Code: SSRSADUserLookup v1.0

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

You must be logged in to post a comment.

Powered by WordPress