Skip Ribbon Commands
Skip to main content

Robin | zevenseas | SharePoint Blog

:

The zevenseas Community > Blogs > Robin | zevenseas | SharePoint Blog > Posts > Little blackbox called WebConfigModifications
September 02
Little blackbox called WebConfigModifications

Some things in SharePoint are pretty hard to gain insight on, especially while doing support, and in my opinion the WebConfigModifications is really one of them.

Why? Well.. if something is not right in there it could crash your WebApplication and there is not much you (as an admin) can do about it right?

Even we as developers have a hard time of getting this right. So that’s why I’ve developed YAAP (Yet Another Application Page) that displays per WebApplication what all the WebConfigModifications are and the best thing is.. you can delete a modification as well (bet you didn’t see that one coming ;) !

Here’s how it looks like :

webconfigmodifications

 

And here’s the code

public class Settings : ApplicationsManagementPage
{
    protected SPGridView gridView;
    protected WebApplicationSelector webApplicationSelector;
    protected override void CreateChildControls()
    {
        if (webApplicationSelector != null)
        {
            SPWebApplication webApplication = webApplicationSelector.CurrentItem;
            gridView.DataSource = GetAllWebConfigModifications();
            gridView.DataBind();
        }            
        base.CreateChildControls();
    }
           
    public void DeleteLink(object sender, EventArgs e)
    {
        foreach (string modificationid in RetrieveIDs())
        {
            if (!string.IsNullOrEmpty(modificationid))
            {
                DeleteModification(int.Parse(modificationid));
            }
        }
    }
    private string[] RetrieveIDs()
    {
        string[] modificationIds = null;

if (((base.Request != null) && (base.Request["ctl00$PlaceHolderMain$HiddenSiteSelections"] != null))

&& !string.IsNullOrEmpty(base.Request["ctl00$PlaceHolderMain$HiddenSiteSelections"]))

        {

modificationIds =

base.Request["ctl00$PlaceHolderMain$HiddenSiteSelections"].ToString().Split(new Char[] { '#' });

        }
        return modificationIds;
    }
    private DataTable GetAllWebConfigModifications()
    {
        //I make use of a DataTable to add an indexer to each SPWebConfigModification object, 
        //by making use of an indexer I make sure that whenever there are multiple entries that 
        //are the same I remove the right one
        DataTable dataTable = new DataTable();
        dataTable.Columns.Add("Id");
        dataTable.Columns.Add("Name");
        dataTable.Columns.Add("Path");
        dataTable.Columns.Add("Owner");
        dataTable.Columns.Add("Sequence");
        dataTable.Columns.Add("Type");
        dataTable.Columns.Add("Value");
        int webConfigModificationId = 1;
        foreach (SPWebConfigModification modification in webApplicationSelector.CurrentItem.WebConfigModifications)
        {                
            DataRow row = dataTable.NewRow();
            row["Id"] = webConfigModificationId.ToString();
            row["Name"] = modification.Name;
            row["Path"] = modification.Path;
            row["Owner"] = modification.Owner;
            row["Sequence"] = modification.Sequence;
            row["Type"] = modification.Type;
            row["Value"] = modification.Value;
            dataTable.Rows.Add(row);
            
            webConfigModificationId++;
        }
        return dataTable;
    }
    private void DeleteModification(int modificationId)
    {
        DataTable modifications = GetAllWebConfigModifications();
        
        SPWebApplication webApplication = webApplicationSelector.CurrentItem;
        webApplication.WebConfigModifications.RemoveAt(modificationId);
        webApplication.Update();
        webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications(); 
    }

 

And that’s it! Hopefully this will help someone out ;)

Will put the solution up on CodePlex this week so you can download it and see for yourself what all the modifications are in for your WebApplications!

Comments

Jeroen Ritmeijer

Excellent, was about to write a tool like that myself.

So many applications, including some famous commercial ones, leave rubbish in the web.config file that stop the system from functioning.

To make matters worse, these changes are re-applied after you delete them manually from the web.config.
System Account on 02/09/2009 00:34

Robin

That's exactly the reason why I built it Jeroen.. that re-applying of modifications without the support of removing them to stop the process.
System Account on 02/09/2009 02:00

Bram

Looks sweet, my compliments. Easy coding, but real added value!!
System Account on 02/09/2009 02:06

Jaap Vossers

Hey Robin,

I feel honoured that you named the project after me ;)

Nice one!
System Account on 03/09/2009 03:25

Robin

Hey Jaap,

glad you feel that way.. was afraid you would be offended by it ;)

How's life in the UK? Going to Vegas in okt?
System Account on 03/09/2009 06:31
 

 Statistics

 
Views: 1791
Comments: 5
Tags:
Published:885 Days Ago