Home > ASP.NET > Hide export format in RDLC/SSRS Report Viewer control

Hide export format in RDLC/SSRS Report Viewer control

I recently needed to hide or disable PDF format in Export Formats dropdown list displayed in SSRS ReportViewer’s toolbar in Local Report mode.

Unfortunately, ReportViewer control has no method or property to manage specific format’s visibility and this is true also for the recently released Report Viewer 10.

We can use Report Viewer’s property ShowExportControls to hide the export formats dropdown list but not a specific format such as PDF.

In Local Report mode, there are 2 export formats (Excel and PDF) and more Rendering Extensions for Server Report mode are available.

We have the option to hide export formats control and implement our own list. this article might help on this.

Finally, I ended up with adding new extension method SetExportFormatVisibility to ReportViewer control which uses private Reflection to disable/enable export formats.

This method is inspired by Stephen Songer’s blog post Disable/Enable export format in SSRS and ASP.NET.

The code consists of two parts, one is ReportViewerExtensions class which has SetExportFormatVisibility extension method and the second is ReportViewerExportFormat Enum to specify available export formats.

public static class ReportViewerExtensions
 {
 public static void SetExportFormatVisibility(this ReportViewer viewer, ReportViewerExportFormat format, bool isVisible)
 {

 string formatName = format.ToString();

 const System.Reflection.BindingFlags Flags = System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance;
 System.Reflection.FieldInfo m_previewService = viewer.LocalReport.GetType().GetField("m_previewService", Flags);

 System.Reflection.MethodInfo ListRenderingExtensions = m_previewService.FieldType.GetMethod("ListRenderingExtensions", Flags);
 object previewServiceInstance = m_previewService.GetValue(viewer.LocalReport);

 IList extensions = (IList)ListRenderingExtensions.Invoke(previewServiceInstance, null);
 System.Reflection.PropertyInfo name = extensions[0].GetType().GetProperty("Name", Flags);

 //object extension = null;
 foreach (var ext in extensions)
 {

 if ((string.Compare(name.GetValue(ext, null).ToString(), formatName, true) == 0))
 {
 System.Reflection.FieldInfo m_isVisible = ext.GetType().GetField("m_isVisible", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

 System.Reflection.FieldInfo m_isExposedExternally = ext.GetType().GetField("m_isExposedExternally", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
 m_isVisible.SetValue(ext, isVisible);
 m_isExposedExternally.SetValue(ext, isVisible);

 break;
 }

 }
 }

 }

 public enum ReportViewerExportFormat
 {
 Excel,
 PDF
 }

Simple to use..

 ReportViewer1.SetExportFormatVisibility(ReportViewerExportFormat.PDF, false); 

Shout it kick it on DotNetKicks.com Retweet

About these ads
Categories: ASP.NET Tags: , , ,
  1. Programmer
    September 20, 2010 at 10:56 am | #1

    Thanks Ahmed Elbaz
    your article is very help full , its magic

  2. Iori
    April 26, 2011 at 5:33 am | #2

    i only wanna to say,thank you a lot!!!!!!you saved me a lot!!!

  3. Paul
    June 2, 2011 at 1:18 pm | #3

    Thanks for your article. But, I can’t able to hide Export to PDF option from Drillthrough event.

    I’m calling report2.rdlc file from report1.rdlc upon clicking the link which is in report1.rdlc file. The below said code used for hiding the PDF option inside Drillthrough event.

    report2.SetExportFormatVisibility(ReportViewerExportFormat.Excel, false);

    but, still the export to PDF option appearing in the report. Please help me out of this problem.

  4. saikat
    September 23, 2011 at 3:14 pm | #4

    thanks a lot.

  5. Suman
    October 10, 2011 at 3:31 pm | #5

    appreciate your help..thanks saved lot of my time..

  6. October 10, 2011 at 5:00 pm | #6

    Thanks that was just what I needed!

  7. riddhi
    December 20, 2011 at 11:36 pm | #7

    Hi,
    I need to manipulate export options for reports in remote mode. Also I am using framework 2.0

  8. Kshitiz Kothari
    November 7, 2012 at 12:54 am | #8

    hi, i created a new class file and pasted the above code in that.
    then called the class method from reportviewer page through
    ReportViewer1.SetExportFormatVisibility(ReportViewerExportFormat.PDF, false);

    but still i can see option for PDF exporting.

    Version info :
    Sql Server 2008 R2
    Report Builder 3.0
    Visual Studio 2010 Professional

  9. February 25, 2013 at 1:57 pm | #9

    A simple solution using CSS:
    a[title=Excel] { display: none !important; }
    a[title=PDF] { display: none !important; }

    • Caleb
      August 2, 2013 at 12:11 pm | #10

      thx Joost your solution was so easy and it worked very well

  10. October 25, 2013 at 2:09 pm | #11

    thanks. Its working for me

  11. October 25, 2013 at 2:10 pm | #12

    thank you very much . Its working for me.

  1. May 10, 2010 at 9:31 am | #1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: