Saturday, October 31, 2009

Opening new windows within SQL Server Reporting Services (SSRS) 2005

In reporting services you have the ability to create a jump to link under the navigation tab. This is to help understand how the link works from the Reporting Services site and from your own hosted site.

example  jump to link contruction in SSRS:
=Parameters!Root_URL.Value.ToString()
& "?SourceID=42cde449-6c46-48c4-97dd-5830c3727c88"
& "&Key=" & rtrim(Fields!Key.Value.ToString())


In our scenario we wanted our form details to look the same as it does from our website.  So, there is a redirect within that page that makes it so if the user hits the back button it doesn't work.  So we decided to open the link in a new window. So we added a javascript wrapper around the link to open it in a new window:

example link construction with JavaScript in SSRS:
="javascript:void(window.open('" & Parameters!Root_URL.Value.ToString()
& "?SourceID=42cde449-6c46-48c4-97dd-5830c3727c88"
& "&Key=" & rtrim(Fields!Key.Value.ToString())
& ",'_blank','menubar=no,titlebar=no,status=no,resizable=yes,scrollbars=no,width=800,height=900'))"
This worked well when we were just using The Reporting Services portal to render the report, but we also deploy our reports to another website, but using an iFrame that points back to the report.  The iFrame URL is captured from a link that is appended to the current URL using QueryString parameter. We ran into an issue where the report server parameters were not making it to the iFrame,  make sure that you URL encode the URL in the QuerySting, if you use this method. Good online encoding tool.

In researching this issue we found a couple good links where people discussed solutions to this problem:
sql server reporting services : Open New Window from Iframe
SSRS: HTML Device Information Setting "LinkTarget" is ignored.
LinkTarget seemed to be the answer.  The way that LinkTarget works is that any links on your report will be opened in the window name defined in the LinkTarget.  In our case we chose "_blank". the full QueryParameter name is:  rc:LinkTarget=_blank.

Microsoft definition:

LinkTarget

The target for hyperlinks in the report. You can target a window or frame by providing the name of the window, like LinkTarget=window_name, or you can target a new window using LinkTarget=_blank. Other valid target names include _self, _parent, and _top.
Article: SQL Server BOL: HTML Device Information SSRS

So you can add the LinkTarget to the URL you use on your own hosted site.

Example Host iFrame URL:
http://YourReportServer/ReportServer?/YourReportPath/YourReportName&rs:Command=Render&rc:LinkTarget=_blank
This does work however, it does not give you any control of the window when it is opened. So, if you use the JavaScript in conjunction with the above link it will then open two windows, but your window should show up with the correct windows parameter settings.  You cannot tell the Report URL the size of the window, but you can the JavaScript.  So all we need to do now is close that extra window. We noticed that the SSRS windows opens first and then the javascript runs and opens a new window from there. So, we used more JavaScript in our Jump to Link construction in SSRS.  window.close() will close the current window where the JavaScript is . 

Example adjustment to the link construction in SSRS:
="javascript:void(window.open('" & Parameters!Root_URL.Value.ToString()
& "?SourceID=42cde449-6c46-48c4-97dd-5830c3727c88"
& "&Key=" & rtrim(Fields!Key.Value.ToString())
& ",'_blank','menubar=no,titlebar=no,status=no,resizable=yes,scrollbars=no,width=800,height=900'));
window.close"