How to update SharePoint web.config for ASP.NET ReportViewer control

If you have been trying to use the ASP.NET ReportViewer control in your SharePoint project then you may have found that you need to make several changes to the web.config, including:

  • Remove the existing ReportViewerMessages appSettings
  • Remove the existing ReportViewerWebControl handler
  • Add a new ReportViewerWebControl handler, which references a later version of the DLL than the existing entry above

(Refer to this article for more details on these changes).

A challenge this poses is how to apply these changes in a proper way. SharePoint gives you two options for doing this: using the SPWebConfigModification class, or the supplemental web.config approaches.

Adding the new handler is not difficult. It is the removal of the existing handler and appSettings key that is the problem. This is because the SPWebConfigModification class allows to you add a node to the web.config, but does not allow you to remove one. The supplemental web.config approach does not allow you to target the changes to a particular web app, which is not ideal in this scenario.

After experimenting with several different things and being very close to giving up , it dawned on me that we can use the collection features of the .NET configuration framework to remove the existing handler and appSettings key.

This means that rather than literally remove these nodes from the web.config, we can use the SPWebConfigModification class to add the node <remove name=”ReportViewerWebControl” /> to the handlers section, and the node <remove key=”ReportViewerMessages” /> to the appSettings section. I tested it and this approach works perfectly! Woohoo!

I have included the PowerShell script for this at the end of this article. In case you are wondering why we shouldn’t just manually apply these changes, here are some reasons:

  • You will need to repeat the changes on all SharePoint servers. This is time consuming and error prone.
  • Your manual changes may be lost where configuration or maintenance tasks are performed on the web app, or when a SharePoint patch/update is applied. This is because these tasks may update the web.config, in which case SharePoint will re-apply only the changes it knows about. SharePoint will not know about your changes if they were applied manually.
  • Your manual changes may be lost when someone else deploys web.config changes to the web app using SPWebConfigModification (as they should).
  • Your manual changes will need to be applied to each new server added to the farm. This is sometime done by the server admins without informing the dev team. As a result, your application may randomly fails in a load-balanced environment.
$webApp = Get-SPWebApplication "http://mySite"
$webAppService = $webApp.Farm.Services | Where { $_.TypeName -eq "Microsoft SharePoint Foundation Web Application" }

$modificationOwner = "MYAPP";

##Remove appSettings key 'ReportViewerMessages'
$configModification = New-Object "Microsoft.SharePoint.Administration.SPWebConfigModification"
$configModification.Owner = $modificationOwner
$configModification.Path = "configuration/appSettings"
$configModification.Name = "remove[@key='ReportViewerMessages']";
$configModification.Type = 0 ##EnsureChildNode
$configModification.Value = "<remove key='ReportViewerMessages'/>"
$webApp.WebConfigModifications.Add($configModification)

##Remove handler name 'ReportViewerWebControl'
$configModification = New-Object "Microsoft.SharePoint.Administration.SPWebConfigModification"
$configModification.Owner = $modificationOwner
$configModification.Path = "configuration/system.webServer/handlers"
$configModification.Name = "remove[@name='ReportViewerWebControl']";
$configModification.Type = 0 ##EnsureChildNode
$configModification.Value = "<remove name='ReportViewerWebControl'/>"
$webApp.WebConfigModifications.Add($configModification)

##Add handler name 'ReportViewerWebControlHandler'
$configModification = New-Object "Microsoft.SharePoint.Administration.SPWebConfigModification"
$configModification.Owner = $modificationOwner
$configModification.Path = "configuration/system.webServer/handlers"
$configModification.Name = "add[@name='ReportViewerWebControlHandler']";
$configModification.Type = 0 ##EnsureChildNode
$configModification.Value = "<add name='ReportViewerWebControlHandler' preCondition='integratedMode' verb='*' path='Reserved.ReportViewerWebControl.axd' type='Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' />"
$webApp.WebConfigModifications.Add($configModification)

$webApp.Update()
$webAppService.ApplyWebConfigModifications()
Advertisement

About Bernado

Based in Australia, I am a freelance SharePoint and Dynamics CRM developer. I love developing innovative solutions that address business and everyday problems. Feel free to contact me if you think I can help you with your SharePoint or CRM implementation.
This entry was posted in Report Viewer, SharePoint. Bookmark the permalink.

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 )

Facebook photo

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

Connecting to %s