Pages

Sunday 26 May 2013

How to flush invalid SPWebConfigModifications SharePoint 2010


Occasionally you modify the web.config using the SPWebCongiModification class. Some times the config entries are not removed from the web.config. You can use the code below to exactly do this:

This is an exact copy of the code mentioend by Luis:
http://stackoverflow.com/questions/10732808/how-to-flush-invalid-spwebconfigmodifications

This is for my reference and I take no credit for this solution, 100% credit goes to Luis :)

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

namespace ModTool
{
    class Program
    {
        static void Main(string[] args)
        {

            SPSite site = new SPSite(args[0]);
            SPWebService service = site.WebApplication.Farm.Services.GetValue<SPWebService>();


            if (args.Length == 1 || string.IsNullOrEmpty(args[1]))
            {
                Console.Out.WriteLine("Listing all Mods and Owners");
                foreach (SPWebConfigModification mod in service.WebConfigModifications)
                {
                    Console.Out.WriteLine("Mod:" + mod.Name + ", Owner:" + mod.Owner);
                }
            }
            else
            {
                Console.Out.WriteLine("Removing all mods owner:" + args[1] + ", reference site:" + args[0]);

                List<SPWebConfigModification> toDelete = new List<SPWebConfigModification>();

                foreach (SPWebConfigModification mod in service.WebConfigModifications)
                {
                    if (mod.Owner == args[1])
                    {
                        toDelete.Add(mod);
                    }
                }

                Console.Out.WriteLine("Found " + toDelete.Count + "Mods");



                foreach (SPWebConfigModification mod in toDelete)
                {
                    service.WebConfigModifications.Remove(mod);
                }
                service.Update();
                SPWebService.ContentService.ApplyWebConfigModifications();
                Console.Out.WriteLine("Done!!");
            }
        }
    }
}
 
Usuage:
ModTool http://site - List all the mods for the farm, site is just an entry point
ModTool http://site owner -Deletes all the mods for in the farm forwhich the owner is "owner" 




No comments:

Post a Comment