Moustafa Refaat
Login   Search
Skip Navigation Links
Home
Publications
Service Offerings
Downloads and Samples
My Resume
Endorsements
Contact Me
Books
Technical Articles
Software Packages
Scroll up
Scroll down
BizTalk The Practical Course
Mastering The BizTalk Technical Interview
Soduku:Challenging Puzzels
Scroll up
Scroll down
Design Patterns Review
Software Architecture Basics Review
Simplified BizTalk Content Based Routing for a Pass_Throu data
An Extensible Light Xml Rules Engine Component
Secure Messaging Solution
Create a SQL Database Programmatically
BizTalk Unzip Adapter
Implementing Singleton pattern with BizTalk Orchestrations
Developing BizTalk Custom Adapters
The BizTalk ESB Toolkit 2.0 experience Series
Scroll up
Scroll down
Setting the ESB Toolkit on the 64 Bit Machines
How The ESB Works
Sample Custom Resolver
Scroll up
Scroll down
Recent Training
Scroll up
Scroll down

News List

  • Toronto Code Camp Presentation is uploaded
  • BizTalk: The Practical Course is recommended by Micorsoft
  • Canadian Gigs Network (www.CanadianGigs.Net) a job web site focusing on Canadian Jobs
  • BizTalk Technical Interview Preparation
  • GT-DataSafe© Online Backup for Amazon Storage Services 3.0 is released
  • Soduku Challenging Puzzles
  • BizTalk: The Practical Course
  • Mastering The BizTalk Technical Interview is Published.

Technical Articles

  • SharePoint List Simplified Configuration Store
  • WCF and xsd:choice how to implement!
  • WCF: Refactoring a Plain old XML web service to a WCF Service
  • Setting the ESB Toolkit on the 64 Bit Machines
  • The BizTalk ESB Toolkit 2.0 experience Series
Skip Navigation Links
> Blog entries about: SharePoint
SharePoint List Simplified Configuration Store

This is a sample configuration store that I wrote for a SharePoint implementation that I was working on. The configuration manager has a simple interface that takes two keys (siteID, and Key)  and returns the string associated with these keys here is the interface definition

 

public interface IConfigurationManager

    {

        string GetConfiguration(string siteID, string key);

    }

 

The implementation for the IConfiguration manager expects to find the list URL and the site URL where the list exists(the list can exist in any site) in the application settings of the web.config. The implementation caches the retrieved values which has its own draw backs as changes to the list are not reflected till the cash expires but for this implementation the requirement did not expect lots of changes.

/// <summary>

    ///  Reads the configuration information from the Configuration list

    ///  the URL for the list is stored in application settings in key

    ///   <add key="ConfigListURL" value="/Configuration/Lists/ConfigurationStore" />

    ////  <add key="configWebSiteURLKey"  value="http://localhost/Configuration"/>

    /// 

    /// </summary>

    public class ConfigurationManager: IConfigurationManager

    {

        private const string configListNameKey = "ConfigListURL";

        private const string configWebSiteURLKey = "configWebSiteURLKey";

       

        public ConfigurationManager()

        {

        }

        public string GetConfiguration(string siteID, string key)

        {

            string entry = null;

            bool dirty = false;

            Dictionary<string, Dictionary<string, string>> globalMap = GetMap();

            Dictionary<string, string> siteMap = null;

            if(globalMap.ContainsKey(siteID))

            {

                siteMap = globalMap[siteID];

                if(siteMap.ContainsKey(key))

                {

                entry = siteMap[key];

                }

                if(null == entry)

                {

                    dirty = true;

                    entry = GetConfigEnrtyFromSharePoint(siteID, key);

                    if (entry != null)

                    {

                        siteMap[key] = entry;

                    }

                }

            }

            else

            {

                dirty = true;

                entry = GetConfigEnrtyFromSharePoint(siteID, key);

                siteMap = new Dictionary<string, string>();

                if (entry != null)

                {

                    siteMap[key] = entry;

                }

                globalMap[siteID] = siteMap;

            }

            // update map if need be

            if (dirty)

            {

                HttpContext.Current.Cache[globalMapCacheKey] = globalMap;

            }

            return entry;

        }

        private string GetValue(SPListItem item)

        {

            string value = string.Empty;

            try

            {

                value = item["Value"].ToString();

            

            }

            catch

            {

                Debug.WriteLine("NoValue");

            }

            return value;

        }

 

        const string queryformatstr = "<Where> <And><Eq><FieldRef Name='Title'/><Value Type='Text'>{0}</Value></Eq><Eq><FieldRef Name='SiteID'/><Value Type='Text'>{1}</Value></Eq> </And></Where>";

        private string GetConfigEnrtyFromSharePoint(string siteID, string key)

        {

            string result = null;

            string SiteUrl = WebConfigurationManager.AppSettings[configWebSiteURLKey];

            SPSecurity.RunWithElevatedPrivileges(delegate()

            {

                using (SPSite siteCollection = new SPSite(SiteUrl))

                {

 

                    using (SPWeb rootweb = siteCollection.OpenWeb())

                    {

 

                        string configlistUrl = WebConfigurationManager.AppSettings[configListNameKey];

                        SPList list = null;

                        try

                        {

                            list = rootweb.GetList(configlistUrl);

                            SPQuery oQuery = new SPQuery();

                            oQuery.Query = string.Format(queryformatstr, key, siteID);

                            SPListItemCollection collListItems = list.GetItems(oQuery);

                            if ((null != collListItems) && (collListItems.Count >= 1))

                            {

                                result = GetValue(collListItems[0]);

                            }

                        }

                        catch (Exception ex)

                        {

                            Debug.WriteLine(ex.ToString());

                        }

                    }

                }

            }

            );

            return result;

        }

        const string globalMapCacheKey = "globalConfigurationMapCacheKey";

 

        private Dictionary<string, Dictionary<string, string>> GetMap()

        {

            Dictionary<string, Dictionary<string, string>> map = HttpContext.Current.Cache[globalMapCacheKey] as Dictionary<string, Dictionary<string, string>>;

            if (null == map)

            {

                map = new Dictionary<string, Dictionary<string, string>>();

                HttpContext.Current.Cache[globalMapCacheKey] = map;

            }

            return map;

        }

    }

}

 

The Provider just loads the implementation based on the class name and assembly name of an entry in the application settings in web.config.

// <summary>

    ///  

    /// Class, Assembly

    ///     <add key="configManagerProvider" value="ConfigurationManager, GlobalConfiguration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=22d2dcb173b01686"/>

    /// </summary>

    public class ConfigurationManagerProvider

    {

        private const string configManagerProviderKey = "configManagerProvider";

        static public  IConfigurationManager GetConfigurationManager()

        {

            string str = WebConfigurationManager.AppSettings[configManagerProviderKey];

            int isep = str.IndexOf(',');

            string assemblyName = str.Substring(isep+1);

            string typeName = str.Substring(0,isep);

            Assembly asm = Assembly.Load(assemblyName);

            IConfigurationManager mngr = asm.CreateInstance(typeName) as IConfigurationManager;

            return mngr;

        }

    }

 

The consumer would write code similar to

      IConfigurationManager cm = ConfigurationManagerProvider.GetConfigurationManager();

      lblSiteTitle.Text = cm.GetConfiguration("Site1", "Key1");

 

{26/08/2010 1:47 PM} {0 comments}  {Tags: SharePoint}
Vaughan Online my Latest SharePoint Project

I was working on a project to migrate a SharePoint 2003 implementation to a new 2007 implementation. This new portal involved restructuring, reorganizing and actually a complete a new architecture and UI design. You can see the sample landing page above Very impressive do not you think so? I used Ajax, with SharePoint to develop many of the web parts, I will write later more updates about the lessons learned from this Project, specially the Migration process.
{30/11/2009 8:30 AM} {0 comments}  {Tags: SharePoint, Web Developmenet}

Copyright © Moustafa Refaat 2004 - 2009. All Rights Reserved.