Jaa


How to: Retrieve a Configuration Setting

The following procedure demonstrates how you can retrieve a configuration setting that you created through the Application Setting Manager.

To retrieve a configuration setting

  1. Add a reference to the SharePoint Guidance Library assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click the Browse tab, and then navigate to the location of the Microsoft.Practices.SharePoint.Common.dll assembly.

  2. Using the same procedure, add a reference to the Microsoft.Practices.ServiceLocation.dll assembly.

  3. Add the following using statements to the top of your source code file.

    using Microsoft.Practices.ServiceLocation;            
    using Microsoft.Practices.SharePoint.Common.Configuration;
    using Microsoft.Practices.SharePoint.Common.ServiceLocation;
    
  4. Use the SharePointServiceLocator.GetCurrent() property to get a reference to the current service locator instance.

    IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();
    
  5. Use the service locator to request an implementation of the IHierarchicalConfig interface.

    IHierarchicalConfig config = 
     serviceLocator.GetInstance<IHierarchicalConfig>();
    
  6. Create an object of the same type as the stored configuration data. You will use this to store the object you retrieve.

    DateTime lastUpdate;
    
  7. (Optional) If your code is running in an environment where the SPContext.Current property is not available, call the SetWeb method and pass in an SPWeb object from which to build the storage hierarchy. If a SharePoint context exists, you can skip this step.

    config.SetWeb(web);
    
  8. Call the IHierarchicalConfig.ContainsKey method to verify that your configuration data exists.

    if(config.ContainsKey("MyApplication.LastUpdate"))
    
  9. If the ContainsKey method returns true, call the IHierarchicalConfig.GetByKey method to retrieve the configuration data. Set the type parameter to the type of the object you want to retrieve.

    lastUpdate = config.GetByKey<DateTime>("MyApplication.LastUpdate");