none
storing custom objects in sharepoint farm property bags RRS feed

  • Question

  • Hello,

    I just wanted to know how can i save a custom object in sharepoint farm property bag.

    eg say we have a class as below how can i save a instance of this class in the farm property bag.

           

      class test
        {
            public int i;
            public int p;

            public test(int i,int p)
            {
                this.i = i;
                this.p = p;

    }

    }

    var t = new Test(3,4);

    How do I save t in the property bag.

    Thanks

    Thursday, July 5, 2012 6:21 PM

Answers

  • Here is a snippet you can use to add your class to the SPFarm's property bag.

    SPFarm farm = SPFarm.Local;
    farm.Properties["MyData"] = new test(1, 2);
    
    farm.Update(true); //Save changes


    Determination conquers all things.

    Thursday, July 5, 2012 7:05 PM
  • Hello,
    You can use the Sharepoint farm property bag as below

    Add the property farm level
    -----------------------------------
    SPFarm farmObject = SPFarm.Local;
    farmObject .Properties.Add("PropertyKey", "PropertyValue");
    farmObject .Update();

    Get the property value farm level
    -------------------------------------------
    SSPFarm farmObject = SPFarm.Local;
    if (farmObject.Properties != null && farmObject.Properties.Count > 0)
    {
    if (farmObject.Properties.ContainsKey("PropertyKey"))
    {
    String propValue = myWebApplication.Properties["PropertyKey"].ToString();
    }
    }

    Modify the property value farm level
    ---------------------------------------------
    SPFarm farmObject = SPFarm.Local;
    if (farmObject.Properties != null && farmObject.Properties.Count > 0)
    {
    if (farmObject.Properties.ContainsKey("PropertyKey"))
    {
    farmObject.Properties["PropertyKey"] = "New Property Value";
            farmObject.Update();
    }
    }
                


    Hiren Patel

    Friday, July 6, 2012 5:18 AM
  • Hi,

    SharePoint doesn't allow to store any custom class object directly. To store any custom class instance in SharePoint property bag, you should mark the class as serializable like this MyCustomType class.By using the serialization, you need to convert your you object into xml.

    That xml you can use to store in the SPFarm.Properties.

    [System.Serializable]
    public class MyCustomType{
    
      public string StringProperty{
       get;
       set
      }
    
    }


    To add custom class instance in SPFarm property bag.

     MyCustomType instace=new instace("Hello");
     // ConvertToXML function serialize the object into xml.
     string xml = ConvertToXML(instace);
     SPFarm myFarm = SPFarm.Local;
     myFarm.Properties.Add("SPFarmKey", xml);
     myFarm.Update();
    
    
    


    To read the custom object back from property bag.

     MyCustomType instace= null;
     SPFarm myFarm = SPFarm.Local;
     string xml= myFarm.Properties["SPFarmKey"];
     // ConvertToObject function deserialize the xml to object.
     instace = ConvertToObject(xml);
    


    ConvertToXML and ConverToObject are custom function for serialization and deserialization.

    For serialization, please refer below link.

    http://geekswithblogs.net/paulwhitblog/archive/2007/07/20/114076.aspx


    Friday, July 6, 2012 7:06 AM
  • Hi,

    You can store custom settings/objects in SharePoint using either Property bag or SPPersistedObject. Simply, you can think property bag as an alternative of web config appsettings. You can store primitive data type easily using property bag.

    However if you want to store your own custom object (and it seems which is what you are planning for), you should consider using SPPersistedObject. One good link for this is : http://joelblogs.co.uk/2010/10/10/persisting-configuration-data-in-sharepoint-2010-with-sppersistedobject/


    Thanks,
    Sohel Rana
    http://ranaictiu-technicalblog.blogspot.com

    Friday, July 6, 2012 7:16 AM

All replies

  • Here is a snippet you can use to add your class to the SPFarm's property bag.

    SPFarm farm = SPFarm.Local;
    farm.Properties["MyData"] = new test(1, 2);
    
    farm.Update(true); //Save changes


    Determination conquers all things.

    Thursday, July 5, 2012 7:05 PM
  • Hello,
    You can use the Sharepoint farm property bag as below

    Add the property farm level
    -----------------------------------
    SPFarm farmObject = SPFarm.Local;
    farmObject .Properties.Add("PropertyKey", "PropertyValue");
    farmObject .Update();

    Get the property value farm level
    -------------------------------------------
    SSPFarm farmObject = SPFarm.Local;
    if (farmObject.Properties != null && farmObject.Properties.Count > 0)
    {
    if (farmObject.Properties.ContainsKey("PropertyKey"))
    {
    String propValue = myWebApplication.Properties["PropertyKey"].ToString();
    }
    }

    Modify the property value farm level
    ---------------------------------------------
    SPFarm farmObject = SPFarm.Local;
    if (farmObject.Properties != null && farmObject.Properties.Count > 0)
    {
    if (farmObject.Properties.ContainsKey("PropertyKey"))
    {
    farmObject.Properties["PropertyKey"] = "New Property Value";
            farmObject.Update();
    }
    }
                


    Hiren Patel

    Friday, July 6, 2012 5:18 AM
  • Hi,

    SharePoint doesn't allow to store any custom class object directly. To store any custom class instance in SharePoint property bag, you should mark the class as serializable like this MyCustomType class.By using the serialization, you need to convert your you object into xml.

    That xml you can use to store in the SPFarm.Properties.

    [System.Serializable]
    public class MyCustomType{
    
      public string StringProperty{
       get;
       set
      }
    
    }


    To add custom class instance in SPFarm property bag.

     MyCustomType instace=new instace("Hello");
     // ConvertToXML function serialize the object into xml.
     string xml = ConvertToXML(instace);
     SPFarm myFarm = SPFarm.Local;
     myFarm.Properties.Add("SPFarmKey", xml);
     myFarm.Update();
    
    
    


    To read the custom object back from property bag.

     MyCustomType instace= null;
     SPFarm myFarm = SPFarm.Local;
     string xml= myFarm.Properties["SPFarmKey"];
     // ConvertToObject function deserialize the xml to object.
     instace = ConvertToObject(xml);
    


    ConvertToXML and ConverToObject are custom function for serialization and deserialization.

    For serialization, please refer below link.

    http://geekswithblogs.net/paulwhitblog/archive/2007/07/20/114076.aspx


    Friday, July 6, 2012 7:06 AM
  • Hi,

    You can store custom settings/objects in SharePoint using either Property bag or SPPersistedObject. Simply, you can think property bag as an alternative of web config appsettings. You can store primitive data type easily using property bag.

    However if you want to store your own custom object (and it seems which is what you are planning for), you should consider using SPPersistedObject. One good link for this is : http://joelblogs.co.uk/2010/10/10/persisting-configuration-data-in-sharepoint-2010-with-sppersistedobject/


    Thanks,
    Sohel Rana
    http://ranaictiu-technicalblog.blogspot.com

    Friday, July 6, 2012 7:16 AM