ASP.NET

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); 
Advertisement

14 thoughts on “Hide export format in RDLC/SSRS Report Viewer control

  1. Pingback: DotNetShoutout
  2. 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.

  3. 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

  4. Hi. I have used your code, but error is

    System.NullReferenceException: Object reference not set to an instance of an object.
    in the Line 95

    Line 93: Dim m_previewService As System.Reflection.FieldInfo = viewer.LocalReport.[GetType]().GetField(“m_previewService”, Flags)
    Line 94:
    Line 95: Dim ListRenderingExtensions As System.Reflection.MethodInfo = m_previewService.FieldType.GetMethod(“ListRenderingExtensions”, Flags)
    Line 96: Dim previewServiceInstance As Object = m_previewService.GetValue(viewer.LocalReport)
    Line 97:

    I am using reportviewer 11.0, is there any version issue?
    Maideen

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 )

Connecting to %s