Share via


Adding a central admin page for managing applications settings

 

You can add a page to central administration and add an entry to the central administration menu to manage application settings for the farm or web application.  As I reviewed in my previous post, Using the Applications Setting Manager with Web App Config (Developing Applications for SharePoint 2010), the settings for a farm or web application cannot be written to from a content web.  However they can be written to from a page hosted in central administration.  For information on how to add a page and the menu entry to central admin, see How to: Deploy an Application Page to Central Administration.

Once the page is deployed, you'll need to actually add the code to update the settings to central admin.  This example demonstrates a simple interface for configuring settings.  One thing I found is that when developing a page for central admin, you'll often need to reset IIS between each debugging session since it appears things are being cached.  I'd start off getting the administration page visible following the above how-to, and then you can perform the following to actually build out updating settings.

The page I built is pretty simple.  It updates a single known setting as described below:

 <asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">

            Select Web App:<asp:DropDownList ID="DDWebApplications" runat="server" />

            <br />Enter Setting Value:

            <asp:TextBox ID="TextBoxSettingValue" runat="server"></asp:TextBox>    

            <asp:Button Text="Save" ID="ButtonSave" runat="server" OnClick="OnSave" />

</asp:Content>

 

The approach I'll outline works well for simple types.  The application setting manager can also store serialized complex types.  In order to administer these you will need to be able to load the assembly that contains the classes so they can be properly serialized and deserialized.  This post won't go into that detail.

As you can see, I'm a master of UI from the following screen shot of my custom page:

 

Often the value you want to set is on a content web application.  The dropdown list is populated when the page is loaded:

 protected void Page_Load(object sender, EventArgs e)

{

    if (!IsPostBack)

    {

        SetWebApps();

    }

}



 

protected void SetWebApps()

{

    DDWebApplications.Items.Clear();



    var names = new List<string>();

    var ids = new List<Guid>();



    foreach (SPWebApplication webApp in SPWebService.ContentService.WebApplications)

    {
        DDWebApplications.Items.Add(new ListItem(webApp.Name, webApp.Id.ToString()));

    }

}



 

In order to set the value, a user selects the content web they want to set the value for, then enters the value and saves.  The save logic is pretty simple:

 protected void OnSave(object sender, EventArgs args)

{

    try

    {

        var locator = SharePointServiceLocator.GetCurrent();

        var configMgr = locator.GetInstance<IConfigManager>();

        string id = DDWebApplications.SelectedValue;

        var webApp = SPWebService.ContentService.WebApplications[new Guid(id)];



        IPropertyBag bag = new SPWebAppPropertyBag(webApp);

        int valueToSet = int.Parse(TextBoxSettingValue.Text);



        configMgr.SetInPropertyBag(settingName, valueToSet, bag);

    }

    catch (Exception ex)

    {

        SharePointServiceLocator.GetCurrent().GetInstance<ILogger>().LogToOperations(ex);

    }

}

First the code gets the web app ID from the drop down list, creates an instance of that web app, and then creates a property bag based upon the web app.  Under the covers, the property bag uses a SPPersistedObject derived class called WebAppSettingStore to store settings in.  An instance of this class is a child of the web application.  If the persisted doesn't exist and can't be loaded, then one is created and saved when the property bag is constructed.  The setting value is then simply set into the property bag, which will under the covers store the value in the WebAppSettingStore instance.

That's it for building custom properties.  Of course typically there are more settings, and they are more complex to manage, but this blog provides all of the pieces in terms of creating the admin page, getting the objects, and storing the objects.