Remove Export Options from ReportViewer Control
I found thismethodthat will remove export options from a server report when called through ReportViewer. I like this one because you dont need to have a web reference to the SQL serverto loop though each rendering extension. How do you call this method if for example,if wanted to remove the XML format from a particular report? I have tried ReflectivelySetVisibilityFalse("XML"), but no luck. Any clues? private void ReflectivelySetVisibilityFalse(RenderingExtension extension){FieldInfo info = extension.GetType().GetField(m_serverExtension,BindingFlags.NonPublic | BindingFlags.Instance);if (info != null){object actualExtension = info.GetValue(extension);if (actualExtension != null){PropertyInfo propInfo = actualExtension.GetType().GetProperty(Visible);if (propInfo != null && propInfo.CanWrite){propInfo.SetValue(actualExtension, false, null);}}}}
February 26th, 2008 5:41pm

I was able to get it using this method that I found on http://mikemason.ca/2007/04/30/ using System.Reflection;using Microsoft.Reporting.WebForms; public void DisableUnwantedExportFormats() { foreach(RenderingExtension extension in serverReport.ListRenderingExtensions()) { if(extension.Name == "XML" || extension.Name == "IMAGE" || extension.Name == "MHTML") ReflectivelySetVisibilityFalse(extension); } } Along with with one I found at http://jasonharper.wordpress.com/ private void ReflectivelySetVisibilityFalse(RenderingExtension extension){FieldInfo info = extension.GetType().GetField(m_serverExtension,BindingFlags.NonPublic | BindingFlags.Instance);if (info != null){object actualExtension = info.GetValue(extension);if (actualExtension != null){PropertyInfo propInfo = actualExtension.GetType().GetProperty(Visible);if (propInfo != null && propInfo.CanWrite){propInfo.SetValue(actualExtension, false, null);}}}} Thanksto both developers - this is some cool stuff
Free Windows Admin Tool Kit Click here and download it now
February 28th, 2008 5:00pm

Just in case this helps anyone else... I could not get the code from the blogs listed above to work. Possibly because I am using WinForms instead of WebForms. In any case, I did eventually get the code to work and am posting my results. Sorry it is in VB, the project I am currently working on wasstarted in VB rather than C#. Our project requires that XML sometimes be displayed, but only for reports which have an XSL that translates them into MS Word. In these cases, we want the export options to display MS Word rather than XML with Data. In the other cases, the XML export is hidden. Imports System.Reflection Imports Microsoft.Reporting.WinForms Imports Microsoft.SqlServer.ReportingServices2005.Execution Public Class ServerReportDecorator Private serverReport As ServerReport Public Sub New(ByVal reportViewer As ReportViewer) Me.serverReport = reportViewer.ServerReport End Sub Public Sub DisableUnwantedExportFormats(ByVal displayXmlFormat As Boolean) For Each extension As RenderingExtension In serverReport.ListRenderingExtensions() If extension.Name = "IMAGE" Or extension.Name = "MHTML" Then ReflectivelySetVisibilityFalse(extension) ElseIf extension.Name = "XML" AndAlso displayXmlFormat Then ReflectivelySetLocalizedName(extension, "MS Word") ElseIf extension.Name = "XML" Then ReflectivelySetVisibilityFalse(extension) End If Next End Sub Private Sub ReflectivelySetVisibilityFalse(ByVal extension As RenderingExtension) Dim info As FieldInfo = extension.GetType().GetField("m_isVisible", BindingFlags.Instance Or BindingFlags.NonPublic) If Not info Is Nothing Then info.SetValue(extension, False) End If End Sub Private Sub ReflectivelySetLocalizedName(ByVal extension As RenderingExtension, ByVal newName As String) Dim info As FieldInfo = extension.GetType().GetField("m_localizedName", BindingFlags.NonPublic Or BindingFlags.Instance) If Not info Is Nothing Then info.SetValue(extension, newName) End If End Sub End Class
December 1st, 2008 6:29pm

Nice VB version helped me tweak minehad same issue, but nothing was working eventually came up with the following in VB.Using Web Developer Express and code added to the Page LOAD eventreferences added to page, I was unable to access any reference to the Reporting Serviceslike the previous post in VB Imports Microsoft.Reporting.WebFormsImports System.Reflection Dim Extension1 As RenderingExtensionDim Info1 As FieldInfoDim server1 As ServerReport server1 = ReportViewer1.ServerReport For Each Extension1 In server1.ListRenderingExtensions If Trim(Extension1.Name) = "PDF" Then Info1 = Extension1.GetType().GetField("m_serverExtension", BindingFlags.Instance OrBindingFlags.NonPublic) If Not Info1 Is Nothing Then Info1.GetValue(Extension1).visible() = False End If End IfNext Ken
Free Windows Admin Tool Kit Click here and download it now
February 11th, 2009 3:32pm

As I'm using the LocalReport (not the ServerReport) I used the following code to remove the PDF Export: Dim Extension1 As RenderingExtension Dim server1 As LocalReport server1 = ReportViewer1.LocalReport For Each Extension1 In server1.ListRenderingExtensions If Trim(Extension1.Name) = "PDF" Then Extension1.[GetType]().GetField("m_isVisible", BindingFlags.NonPublic Or BindingFlags.Instance).SetValue(Extension1, False) End If Next
May 13th, 2009 3:37am

Thanks a lot. I was also facing similar kind of problem and your code help me a lot to solve it. Thanks & Regards,Nirmal Doshi, MCTS| Systems Plus Solutions, Indiawww.systems-plus.com| www.ondemandsharepoint.com
Free Windows Admin Tool Kit Click here and download it now
July 13th, 2009 7:01am

I stumbled across similar challenge today using VS 2008 and SQL 2005 (9.00.4035.00). I found out that the m_serverExtension actually does not exists nor Visible. So just for a quick reference please find the code below. Bear in mind you will have to reference System.Reflection namespace. public void DisableUnwantedExportFormats() { FieldInfo info; foreach (RenderingExtension extension in reportViewer.ServerReport.ListRenderingExtensions()) { if (extension.Name != "PDF" && extension.Name != "EXCEL") // only PDF and Excel - remove the other options { info = extension.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic); info.SetValue(extension, false); } if (extension.Name == "EXCEL") // change "Excel" name on the list to "Excel 97-2003 Workbook" { info = extension.GetType().GetField("m_localizedName", BindingFlags.Instance | BindingFlags.NonPublic); if (info != null) info.SetValue(extension, "Excel 97-2003 Workbook"); } } } Hope this helps! RK
November 11th, 2009 3:01pm

Hi Rob, I've tried by adding your method DisableUnwantedExportFormats() for hiding Export to Excel option. When report loaded first time the excel option not getting visible. However, When I used to call the same method inside of Drillthrough() event "Excel" & PDF option getting visible in Export controls dropdown. I've tried by calling your method in the first statement of my Drillthrough() event,(like what I used in Page load method). Please let me know, How can I hide the excel option in Drillthrough() event of Reportviewer. Thanks. Paul
Free Windows Admin Tool Kit Click here and download it now
June 3rd, 2011 6:57am

Below is the version of what I have come up with and Hope it Helps someone. This version is just a tweak from the above code for localreports I am using WinForms, Visual Studio 2010 and RDLC (Localreport) and I only want to hide PDF format as it does not export correctly Imports System.Reflection Imports Microsoft.Reporting.WinForms Imports Microsoft.ReportingServices Public Class LocalReportDecorator Private _localReport As LocalReport Public Sub New(ByVal reportViewer As ReportViewer) _localReport = reportViewer.LocalReport End Sub Public Sub HidePDFFormat() For Each extension As RenderingExtension In _localReport.ListRenderingExtensions() If extension.Name = "PDF" Then ReflectivelySetVisibilityFalse(extension) Exit For End If Next End Sub Private Sub ReflectivelySetVisibilityFalse(ByVal extension As RenderingExtension) Dim info As FieldInfo = extension.GetType().GetField("m_isVisible", BindingFlags.Instance Or BindingFlags.NonPublic) If Not info Is Nothing Then info.SetValue(extension, False) End If End Sub End Class 'Usage: ' This Should be called after ReportViewer1.Reset and Before ReportViewer1.Refresh Dim srd As New LocalReportDecorator(ReportViewer1) srd.HidePDFFormat()
June 25th, 2012 9:42am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics