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

Doing some testing of adding multiple configuration settings during a feature activation. One of my trials caused an error with a setting, specifically around preCondition statement for system.webServer.

Now every time I try to run ApplyWebConfigModifications() it fails on activate for this particular error encountered previous.

I'm at a point where I created a completely new SharePoint project, create a completely new feature receiver and tried to do a basic webapp.Update() and a call to webapp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications() but still it fails on that particular error encountered.

The error:

Error   1   Error occurred in deployment step 'Activate Features': 'add [@name="ExceptionModule"] [@type="University.SPErrorHandler.ExceptionModule, University.SPErrorHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c0dd6b3f4715d787" preCondition="integratedMode"]' has an invalid token.
        0   0   CustomException.Module

My feature activated method:

/// <summary>
/// handle the event raised after a feature has been activated
/// </summary>
/// <param name="properties">a collection of receiver properties</param>
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWebApplication webapp = (properties.Feature.Parent as SPSite).WebApplication;
    webapp.Update();
    webapp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}

as you can see, I'm not even referencing a web modification item within my activated.

Is there a know way to get rid of this annoying failure? I've tried a system reboot, IIS reset, even tried creating a new web application to deploy against, still the same issue.

Update

The answer to this was pretty simple. Like any other collection property, it needs to be cleared (ensured it contains no values) before you add a new set of items to it. SPWebConfigModications is a collection object, so calling Clear() on it ensures that new items are allows added to an empty instance (not null).

Solution is then (in my case, my feature is Site scoped):

// get web application
SPWebApplication webapp = (properties.Features.Parent as SPSite).WebApplication;

// clear SPWebConfigModifications collection
webapp.WebConfigModiciations.Clear();

// TODO: add your modifications

// update the web application
webapp.Update();

// apply modifications to the farm
webapp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
share|improve this question
    
My understanding is that the WebConfigModifications collection is a "permanent" collection of all the changes from the stock web.config, so that by using Clear() you would also undo changes that other features have made. This may be fine (or even necessary, if you had something buggy in there) in your scenario, but may cause unintended side effects for others. (p.s. it's preferable to create an answer rather than editing the question when you've found an answer on your own). –  lgaud Jul 12 '13 at 18:37

1 Answer 1

i would suggest to store your configuration key/values NOT in web.config but in a property bags (e.g. http://msdn.microsoft.com/en-us/library/ff798340.aspx), because the handling is much more easy.

but this would cause a redesign of your application.

share|improve this answer
2  
hi daniel, thanks, but i should have been clearer. i want to store configuration specific entries during feature activation and remove them during deactivation, like HTTP handlers, HTTP modules, etc. These cannot be stored anywhere else. –  JadedEric Feb 19 '13 at 6:08

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.