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 27th, 2008 1:41am

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 29th, 2008 1:00am

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 2nd, 2008 2:29am

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 11: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 10: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 2:01pm

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 11: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 7:00am

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

Other recent topics Other recent topics