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();