Create Hierarchical Data Store in SharePoint 2013

Hierarchical Data Store

SharePoint 2013 allows Hierarchical Data Storage using the SPPersistedObject class. Hierarchy means we can store key/value pairs using a parent/child hierarchy.

Hierarchical Data Store allows global storage and thus a solution spanned to multiple-sites can store common data without the need to remember the location.



Example

Step 1: Create Project

Create a Farm Solution and add a Visual Web Part into it.



Step 2: Create Class

Create a class as in the following.

  1. namespace HierachicalObject_WebPart  
  2. {  
  3.     [Guid("0A93ED9E-04DB-4754-B463-C91E542D1308")]  
  4.     public class MyAppSettings : SPPersistedObject  
  5.     {  
  6.          public MyAppSettings()  
  7.     {  
  8.     }  
  9.     public MyAppSettings(string name, SPPersistedObject parent)  
  10.         : base(name, parent)  
  11.     {  
  12.     }  
  13.     public MyAppSettings(string name, SPPersistedObject parent, Guid id)  
  14.         : base(name, parent, id)  
  15.     {  
  16.     }  
  17.   
  18.         [Persisted]  
  19.         private string _databaseName;  
  20.   
  21.         public string DatabaseName  
  22.         {  
  23.             get { return _databaseName; }  
  24.             set { _databaseName = value; }  
  25.         }  
  26.   
  27.         protected override bool HasAdditionalUpdateAccess()  
  28.         {  
  29.             return true;  
  30.         }  
  31.     }  
  32.   

The class does the following:

  1. Specifies 2 constructors
  2. Creates 1 property
  3. Marks 1 field for persistence
  4. Enables override for data updates

Step 3: Save and Load Code

Create 2 buttons on the visual web part and add the following code in their click events.

  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3.     MyAppSettings myapp = new MyAppSettings("MyAppSettings", SPFarm.Local);  
  4.     myapp.DatabaseName = TextBox1.Text;  
  5.     myapp.Update(true);  
  6. }  
  7.   
  8. protected void Button2_Click(object sender, EventArgs e)  
  9. {  
  10.     SPSite site = SPContext.Current.Site;  
  11.     MyAppSettings myapp = SPFarm.Local.GetChild<MyAppSettings>("MyAppSettings");  
  12.     Label1.Text = myapp.DatabaseName;  
  13.     myapp.Delete();  

Step 4: Test the Code

You can run the solution, create a new test page and add the visual web part to it. You can see the Load and Save working if everything went well.


Advantages

To summarize the advantages of Hierarchical Data Store:

  1. Configuration values can be stored for global access
  2. Data can be stored in a hierarchical manner
  3. Custom Properties can be created
  4. Data is saved in XML serialization
  5. Content is persisted on backups

References

https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.sppersistedobject.aspx

Summary

This article explored Hierarchical Data Store in SharePoint 2013. You can refer to the source code attached.