3/17/2010

How to Store Settings in SharePoint

When you create SharePoint solutions or features, you are likely to have to store some settings. Here's a good way to manage the settings in an encapsulated, type-safe way:

Create a class that encapsulates all the properties that comprise your setting and takes care of saving and retrieving these settings. For this example, I name this class MySettings. Here's how you would work with the class:

// load settings



MySettings settings = MySettings.Read();



// read and manupulate



string foo = settings.setting1;



settings.setting2++;



// save updated settings



settings.Save();




Below is the code for the sample class. This class saves settings at the site collection root level. You may want to change the code to store it at the web application level or the site level.









using System;



using System.Collections.Generic;



using System.Text; 



 



using Microsoft.SharePoint; 



 



using System.Web;



using System.Data; 



 



using System.IO;



using System.Xml;



using System.Xml.Serialization; 



 



public class MySettings



{



    // use a GUID to store the setting in order to avoid conflicts



    static private string mySettingsKey = "{abe971ed-d09a-4887-86fd-be7816ddebae}";



    public string setting1;



    public int setting2; 



 



    // your settings class can take almost any form, but it needs to have a constructor without any arguments



    public MySettings()



    {



        // set any default values here



        setting1 = "abc";



        setting2 = 0;



    } 



 



    public void Save()



    {



        SPWeb siteCollection = SPContext.Current.Site.RootWeb;



        siteCollection.AllowUnsafeUpdates = true;



        string xml = SerializeToXml(this);



        if (siteCollection.AllProperties.ContainsKey(mySettingsKey))



        {



            siteCollection.AllProperties.Remove(mySettingsKey);



            siteCollection.Update();



        }



        siteCollection.AllProperties.Add(mySettingsKey, xml);



        siteCollection.Update();



        siteCollection.AllowUnsafeUpdates = false;



    }



    public static MySettings Read()



    {



        MySettings retVal = new MySettings();



        SPWeb siteCollection = SPContext.Current.Site.RootWeb;



        if (siteCollection.AllProperties.ContainsKey(mySettingsKey))



        {



            string xml = siteCollection.AllProperties[mySettingsKey];



            retVal = DeserializeFromXml(xml, typeof(MySettings)) as MySettings;



        }



        return retVal;



    } 



 



    public static string SerializeToXml(Object obj)



    {



        XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());



        using (MemoryStream memoryStream = new MemoryStream())



        {



            using (XmlTextWriter xmlWriter = new XmlTextWriter(memoryStream, new UTF8Encoding(false)))



            {



                xmlSerializer.Serialize(xmlWriter, obj);



            }



            return Encoding.UTF8.GetString(memoryStream.GetBuffer());



        }



    } 



 



    public static Object DeserializeFromXml(string strXML, Type objType)



    {



        Object retVal;



        XmlSerializer xmlSerializer = new XmlSerializer(objType);



        using (MemoryStream memoryStream = new MemoryStream())



        {



            byte[] bytes = Encoding.UTF8.GetBytes(strXML);



            memoryStream.Write(bytes, 0, bytes.Length);



            memoryStream.Seek(0, SeekOrigin.Begin);



            using (XmlTextReader xmlReader = new XmlTextReader(memoryStream))



            {



                retVal = xmlSerializer.Deserialize(xmlReader);



            }



        }



        return retVal;



    }



} 


No comments:

adaxas Web Directory