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.
- namespace HierachicalObject_WebPart
- {
- [Guid("0A93ED9E-04DB-4754-B463-C91E542D1308")]
- public class MyAppSettings : SPPersistedObject
- {
- public MyAppSettings()
- {
- }
- public MyAppSettings(string name, SPPersistedObject parent)
- : base(name, parent)
- {
- }
- public MyAppSettings(string name, SPPersistedObject parent, Guid id)
- : base(name, parent, id)
- {
- }
-
- [Persisted]
- private string _databaseName;
-
- public string DatabaseName
- {
- get { return _databaseName; }
- set { _databaseName = value; }
- }
-
- protected override bool HasAdditionalUpdateAccess()
- {
- return true;
- }
- }
-
- }
The class does the following:
- Specifies 2 constructors
- Creates 1 property
- Marks 1 field for persistence
- 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.
- protected void Button1_Click(object sender, EventArgs e)
- {
- MyAppSettings myapp = new MyAppSettings("MyAppSettings", SPFarm.Local);
- myapp.DatabaseName = TextBox1.Text;
- myapp.Update(true);
- }
-
- protected void Button2_Click(object sender, EventArgs e)
- {
- SPSite site = SPContext.Current.Site;
- MyAppSettings myapp = SPFarm.Local.GetChild<MyAppSettings>("MyAppSettings");
- Label1.Text = myapp.DatabaseName;
- 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:
- Configuration values can be stored for global access
- Data can be stored in a hierarchical manner
- Custom Properties can be created
- Data is saved in XML serialization
- 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.