Skip to content

Configuration Component

Andreas Scharf edited this page Sep 24, 2013 · 9 revisions

###Overview There are several good and bad ways of storing your configuration in and around SharePoint.

Method Result
Hard Coded very bad - that's like no configuration at all!
web.config better but still very bad at large farms, even when used with SPWebConfigModification
SP Lists ok, but you have to define where to store (Web, Site, WebApp?) it and save that link somehwere...
SPPersistedObject ok, but quite complex. you have to create your own object for every application, and sometimes Update Conflicts in large farms will happen if not done correctly
Sponge awesome! stores your configs in the Central Admin and makes it accessible via API and Web Services!

Sponge creates a Site with multiple lists in your Central Administration http://myadmin/Sponge:

  • ConfigApplications: The name's of your applications you want to configure. This gives you a nice grouping of the config entries later on!
  • ConfigItems: This is where the actual config goes to. Add your Key/Value pairs to your application and you are ready to go!

###Usage At first, you have to decide which application you want to configure - is it an application that "lives" on SharePoint e.g. WebPart, Timer Job, etc. or is it a client application like WinForms or even an Windows Phone App!

Obviously, Sponge has to be installed on your SharePoint Farm first. See the Installation part of this readme!

  • At first you have to create a Config Application. To do this, go to the Central Administration and Click Config Applications and add an item with your desired app name.

alt text

  • Now go to the Config Items list and start adding your configuration. Example:

alt text

That's it. The configuration for Sponge Log Viewer has been set up and you now can retrieve it with the server- or client side API.

####Server Side In order to use the server side API reference Sponge.dll and add using Sponge.Logging; to your usings.

public void GetConfigServerSideOnline()
{
        //gets the config from central admin
	var cfg = ConfigurationManager.GetOnline("Sponge Log Viewer");
	
        //gets the config from site collection
        var cfg = ConfigurationManager.GetOnline("http://demo", "Sponge Log Viewer");

	//print out all items from the configuration
	foreach(var entry in cfg.Items) 
        {
             Console.WriteLine("{0} - {1}", entry.Key, entry.Value);
        }
       
        //get the value of MyKey as integer
        int a = cfg.Get<int>("MyKey");
}

####Client Side In order to use the server side API reference Sponge.Client.dll and add using Sponge.Client.Logging; to your usings.

public void GetConfigClientSide()
{
        //url to the Site Collection
        var spongeUrl = "http://demo";

        //name of the app in sharepoint
        var app = "Sponge Log Viewer";

        //whether the ClientConfigurationManager should retrieve the log config from central admin (true) or from the relative Sponge Admin Page (false)
        //relative means --> "http://demo/Sponge" --> Sponge Logging & Configuration Component Feature has to be enabled in this Site Collection
        var central = false;

        //create the logger instance
	var cfg= ClientConfigurationManager.GetOnline(spongeUrl, app, central);
	
        //print out all items from the configuration
	foreach(var entry in cfg.Items) 
        {
             Console.WriteLine("{0} - {1}", entry.Key, entry.Value);
        }
}

###Supported API

####Server Side

  • GetOnline(string appName): This gets the config for the specified appName from Sponge Central Admin.

  • GetOnline(string siteCollUrl, string appName): This ges the config from the specified siteCollectionUrl. Note: Sponge Logging & Configuration Component Feature has to be enabled in this Site Collection.

####Client Side

  • GetOnline(string siteCollUrl, string loggerName, bool central): This gets the config from either Sponge Central Admin (central = true) or from siteCollUrl's Sponge Admin (central = false) Site. Note: Sponge Logging & Configuration Component Feature has to be enabled in this Site Collection.

Back to Home.