3

I have a 3rd party control that i want to add to the web part. That 3rd party control uses a generic ASHX handler. I can always add the registration to the actual WFE web.config but it's a bit manual to my tastes. Does anyone succeeded to add to web.config using a deployment package?
i would need something like this:

<system.webServer>
   <handlers>
      <remove name="SomeHandler" />
      <add name="SomeHandler" preCondition="IntegratedMode" 
           verb="GET" path="SomeHandler.ashx" type="SomeHandler, SomeHandler" />
   </handler>
</system.webServer>

Update
Here is the code that I ended up creating in the feature receiver:

var modifications = new List<SPWebConfigModification>()
                    {
                            new SPWebConfigModification("remove[@name='BotDetectCaptachHandler'",
                                                        "configuration/system.webServer/handlers")
                            {
                                Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                                Value = @"<remove name='BotDetectCaptchaHandler'/>"
                            },
                            new SPWebConfigModification("add[@name='BotDetectCaptchaHandler'",
                                                        "configuration/system.webServer/handlers")
                            {
                                Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                                Value = @"<add name='BotDetectCaptchaHandler' preCondition='integratedMode' verb='GET' path='BotDetectCaptcha.ashx' type='BotDetect.Web.CaptchaHandler, BotDetect' />"
                            }
                    };

SPSecurity.RunWithElevatedPrivileges( () =>
                         {
                               var contentService = SPWebService.ContentService;
                               foreach (var modification in modifications)
                               {
                                   contentService.WebConfigModifications.Add(modification);
                                }
                                contentService.Update();
                                contentService.ApplyWebConfigModifications();
                          });

When I do activate i get an error message saying that i have previously queue modifications failing to execute. Does anyone know how to query queued modifications and remove them? I tried PowerShell but was not successful. It seems that SPWebApplication.WebConfigModifications collection is always empty.

2
  • 1
    I think you might need to make sure you are setting the owner on those modifications so when you deactivate the feature the web.config changes get undone. I realize i forgot to put that in my code below. Commented Apr 13, 2012 at 0:30
  • @SteveLineberry, good point. I figured if I leave the owner portion out, it'll assign the default value and that value is used during deactivation. My problem now is that i have trouble finding those queued up modifications b/c error is talking about configuration/SafeControls/... and i am not even setting that portion of the config. I am chasing ghosts for now ! :)
    – Roman
    Commented Apr 13, 2012 at 5:49

1 Answer 1

6

I would use a SPWebConfigModification: http://msdn.microsoft.com/en-us/library/bb861909.aspx

Something similar to this:

SPWebConfigModification handlerMod = new SPWebConfigModification();
handlerMod.Path = "configuration/system.webServer/handlers";
handlerMod.Name = "add[@name='SomeHandler'][@type='SomeHandler']";
handlerMod.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
handlerMod.Value = "<add name=\"SomeHandler\" preCondition=\"IntegratedMode\" verb=\"GET\" path=\"SomeHandler.ashx\" type=\"SomeHandler, SomeHandler\" />";
3
  • 2
    +1 however it is worth mentioning that this will need to be deployed in a web application scoped feature. Also, be very careful with SPWebConfigModification. I would highly recommend reading Mark Wagner's entire blog post. Make sure you test it in DEV first!
    – Kit Menke
    Commented Apr 12, 2012 at 1:53
  • 1
    Yes. I ended up working on that. I'll post a follow up shortly as web config modifications are the most tedious and error prone.
    – Roman
    Commented Apr 12, 2012 at 21:17
  • @Steve Lineberry, the only issue that i have at this point is that for some reason web.config modification logic reorders the modifications. In handler's section, doing a remove followed by add is normal, and I cannot achieve it.
    – Roman
    Commented May 23, 2012 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.