Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
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. –  Steve Lineberry Apr 13 '12 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 Apr 13 '12 at 5:49

1 Answer 1

up vote 5 down vote accepted

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\" />";
share|improve this answer
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 Apr 12 '12 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 Apr 12 '12 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 May 23 '12 at 17:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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