atxgeek 


just one more geek in a sea of austin techies

February 8, 2015

Set SSRS export options for individual reports #ReportGeek

File type export options in
Microsoft SSRS
If you work with Microsoft's SQL Server Reporting Services (SSRS) then you've probably run across a certain irritating limitation:  no ability to define the export file type options for individual reports.

Although the Reporting Services product is now over a decade old, Microsoft has failed to add some of the more obvious features any small startup would likely jump to support.  One of those features is granular control over export options.  You would expect that the file types provided in a report's export options could be defined specifically for that report.  Barring that, you might expect that the export options could be defined at the report folder level such as is done for security rights.

Alas, no, Microsoft has to be difficult by providing only a system-wide setting for report file type export options.  That means that, if you rely on the default Microsoft ReportViewer control, every report in the entire SSRS server is bound by the same export configuration.

Take Control
I'm going to show you how to easily control the mix of export options in the ReportViewer control using a handy bit of CSS (only one line of code!)  From there I'll point you towards a solution for controlling export options on individual reports.

The Solution
Skipping right ahead to the actual code, here is a CSS-based solution that simply hides all export options except PDF and Excel:

Export options hidden with CSS
leaving only PDF and Excel.
<style>
  a[onclick*='exportReport(\'CSV\');'],
  a[onclick*='exportReport(\'IMAGE\');'],
  a[onclick*='exportReport(\'MHTML\');'],
  a[onclick*='exportReport(\'WORD\');'],
  a[onclick*='exportReport(\'XML\');']
  {display:none !important}
</style>

This CSS overrides the "display" property of any link that features an "onclick" attribute containing the characters "exportReport('<filetype>')".  The filetype *must* match the particular string used by Microsoft and the filetype *must* be capitalized.  For instance, the "TIFF" filetype is represented in code as "IMAGE" so the above CSS looks for "IMAGE" rather than "TIFF".

The end result is that the ReportViewer control still includes all filetypes allowed by the SSRS server's global configuration but, based on our custom CSS definition (that you must include on the web page), you control which options actually get displayed.


But my users go directly to the Report Server...
Perhaps you're not using the ReportViewer control on a page within a custom website.  Perhaps you are instead allowing users to browse directly to the Report Server and use Microsoft's web interface to view and export reports.  How do you apply custom CSS to that?

Bad news:
You won't be able to add CSS to individual pages within the report server.

Good news:
You *can* apply custom CSS to the entire report server and tailor that CSS to target individual report server folders (pages) as appropriate.

Where the CSS goes:
We'll add our custom CSS to the existing Report Server CSS file "ReportingServices.css" located under the "/RSM/STYLES" folder (you'll need access to the report server's file system).

If you apply the CSS as in the example above it will affect all reports -- we need to reduce the scope in order to target a particular folder.  When viewing a report page, the SSRS server conveniently places the report's path and the name of the report into an "action" attribute within the "form" tag.  That attribute looks something like this:

 action="ReportViewer.aspx?%2fSandbox%2fMy+Chart&rs%3aCommand=Render"

From this value we can derive that the server is currently displaying a report named "My Chart" located in the folder "Sandbox".  Armed with this information, we can tailor our custom CSS to target this specific report:


  form[action*=
    'ReportViewer.aspx?%2fSandbox%2fMy+Chart&']
  a[onclick*='exportReport(\'CSV\');'],
  form[action*=
    'ReportViewer.aspx?%2fSandbox%2fMy+Chart&']
  a[onclick*='exportReport(\'IMAGE\');'],
  form[action*=
    'ReportViewer.aspx?%2fSandbox%2fMy+Chart&']
  a[onclick*='exportReport(\'MHTML\');'],
  form[action*=
    'ReportViewer.aspx?%2fSandbox%2fMy+Chart&']
  a[onclick*='exportReport(\'WORD\');'],
  form[action*=
    'ReportViewer.aspx?%2fSandbox%2fMy+Chart&']
  a[onclick*='exportReport(\'XML\');']
  {display:none !important}


This CSS has added a "form" tag selector as a prerequisite to the "a" selector we had before.  Our CSS will now only apply to matching "a" tags that are also descendants of a "form" tag with an "action" attribute containing the string shown above.

Apply CSS to an entire Report Server folder
Instead of a single report, we could apply the export options to every report in the "sandbox" folder by dropping the "My Chart" reference:


  form[action*='ReportViewer.aspx?%2fSandbox%2f']
  a[onclick*='exportReport(\'CSV\');'],
  form[action*='ReportViewer.aspx?%2fSandbox%2f']
  a[onclick*='exportReport(\'IMAGE\');'],
  form[action*='ReportViewer.aspx?%2fSandbox%2f']
  a[onclick*='exportReport(\'MHTML\');'],
  form[action*='ReportViewer.aspx?%2fSandbox%2f']
  a[onclick*='exportReport(\'WORD\');'],
  form[action*='ReportViewer.aspx?%2fSandbox%2f']
  a[onclick*='exportReport(\'XML\');']
  {display:none !important}


Why did I leave the "%2f" (the value for a slash character) on the end of the matching string here and the "&" symbol on the end in the previous example?  Quite simply, I'm relying on these special characters as punctuation.  By including the "%2F" my CSS will be able to distinguish between a folder named "Sandbox" and a folder named "Sandbox Part 2" or "Sandboxing" or "Sandbox Deluxe".  Similarly, in the previous example the inclusion of the "&" means the CSS will apply to the report "My Chart" but not to "My Chart2" or "My Charts" etc.


File Types
For reference, here are the export file types and the matching "onclick" values you'll need to reference in the custom CSS:


File type CSS string
CSV CSV
EXCEL EXCEL
MHTML MHTML
PDF PDF
TIFF IMAGE
MS WORD WORD
XML XML


A note regarding "onclick"
Depending on the report viewer control used you may or may not have additional tags and/or attributes you could match your CSS against.  The "onclick" attribute was the only consistent attribute across the report viewer controls I checked.


I realize this solution is only for web-browser-based interfaces.  If you are programming a Windows app or are implementing a ReportViewer control in your own web app then lucky you -- you've got options.  For the non-programmers and/or those who rely on allowing users to browse the report server directly, I hope this post has given you an idea of how to take some control over file export options at the individual report level.  Please comment and share if you've found a better approach!



No comments:

Post a Comment