Tuesday, April 6, 2010

URL Encoding in Reporting Services

Handling Special Characters Using HttpUtility.URLEncode()

By Dan Meyers

Reporting Services allows you to create hyperlinks in your reports using the Go To URL action. This is a very useful feature that provides you with the ability to make almost anything on the report a hyperlink. When creating links to other reports, this action type provides many advantages over using the Go To Report action type. For example, you can embed some javascript into the link so that the report opens up in a new window that is a specific size in a specific location on the screen. You cannot do this using the Go To Report action type.

Of course there are disadvantages too. When using the Go To URL action type you have to manually handle any special characters in you URL string. This is something that the Go To Report action type handles for you when it comes to the parameter values passed to the report. In my opinion, the advantages outweigh the disadvantages. So I always use the Go To URL action type and manually handle the special characters using the HttpUtility.URLEncode() function.

Before we can use the the HttpUtility.URLEncode() function we have to first add a reference to System.Web assembly.

Go to Report –> Properties –> References. Click [Add].


Scroll down a select ‘System.Web’ in the list and click [OK].


You should now see that the reference has been added.


Now that we have added the reference to System.Web, we now have to create a function in the report’s Code window that uses the URLEncode() function. Click on ‘Code’ in the left panel and enter the code below.

Public Function URLEncode (ByVal inURL As String) As String
Dim outURL As String
outURL = System.Web.HttpUtility.UrlEncode(inURL).ToString
Return outURL
End Function


We can now use our new report function in an expression anywhere in our report.


As you can see in the screenshot below, the function will convert any special charters and encode the URL. In this example, I am passing in a string for a MDX member for Barnes & Noble. You can see the before and after values below.


A Common Mistake
One common mistake that many make when taking this approach is that they try to use the HttpUtility.URLEncode() function directly from an expression. The expression will basically ignore the reference and not work. You have to use the function in the report’s Code window and then reference the report’s function in the expression for it to work.


web said...

Nice blog of url encoding.
I like this blog.
When you use one of these characters in a page name, the page name gets converted to a URL in a process called encoding.

The URL encoding process is subject to certain rules. These rules are specified in the RFC entitled RFC 1738: Uniform Resource Locators (URL) specification. This defines that only the characters below are allowed to be used in an URL:

-A to Z
-numeric characters 0 to 9
-and these special characters: $-_.+!*'(),


JC said...

I'm rendering a SSRS report using URL access and I'm facing this problem :

the date is format as yyyymm

doesn't work (error rsInvalidReportParameter)

(and other dates) works fine

Don't you know why only 201010 doesn't work?


Eric Lofstrom said...

The rsInvalidReportParameter error indicates that 201010 is not a value in the parameter's dataset or list of available values. Check the query or list to ensure that the value is available.

Post a Comment