My blog has moved!

Visit http://blog.adamroderick.com/ and update your bookmarks.

Wednesday, July 22, 2009

Easily Fix your SPWebConfigModification Mistakes

The problem with SPWebConfigModification is that it ACTUALLY modifies your web.config. So if you are relying on a Feature to apply and remove your SPWebConfigModifications, that's a problem when you put in a bad modification. Here is a code block to remove the errored web config problem.

1. Create a new C# console application
2. Paste this code
3. Replace "{24D97BF4-E67E-44ae-92E1-39E0029B61FE}" with the .Owner value you supplied
4. Replace "SharePoint - 8083" with the friendly name of your web application, which can be found in Central Admin's web application list.
5. Run and you are done.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.Collections.ObjectModel;

namespace AdamCleanupWebConfigModification
{
class Program
{
// matches the ID in the feature.xml for this feature
private const string FEATURE_ID = "{24D97BF4-E67E-44ae-92E1-39E0029B61FE}";

static void Main(string[] args)
{
SPWebService svc = SPWebService.ContentService;
SPWebApplication webApp = svc.WebApplications["SharePoint - 8083"];
Collection<SPWebConfigModification> collection = webApp.WebConfigModifications;

int iStartCount = collection.Count;

// Remove any modifications that were originally created by the owner.
for (int c = iStartCount - 1; c >= 0; c--)
{
SPWebConfigModification configMod = collection[c];

if (configMod.Owner == FEATURE_ID)
collection.Remove(configMod);
}

// Apply changes only if any items were removed.
if (iStartCount > collection.Count)
{
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
webApp.Update();

Console.WriteLine("Removed " + (iStartCount - collection.Count));

}

Console.WriteLine("Press any key to exit..");
Console.ReadKey();

}
}
}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home