Introduction of SPPersistedObject class



Objective

In this article, I will give a high level introduction of SPPersistedObject class and its uses. I will give one sample also to explain uses of this class.


SPPersistedObject class

  1. This class is inside namespace Microsoft.SharePoint.Administration
  2. This is base class for all administration objects.
  3. It serializes all fields marked with Persisted attribute to XML.
  4. It writes XML BLOB to configuration database.
  5. Configuration data that is stored in persisted object is automatically made available to every process on every server in the farm.

Uses

Consider a scenario when, there is requirement to save configuration information not in Web.config file but in some permanent storage. In this case we will go for SPPersistedObject class.

Example

In this example, I am going to save username and password using SPPersistedObject class. The following example illustrates a custom class that inherits from the SPPersistedObject class where the Peristed attribute is used to specify fields for serialization.

Step 1: Create application

Create an application. For my purpose, I am creating a window application. After creating application add reference of Windows.SharePoint.Services.

Step 2: Create the custom class

Add a class in application. I am giving name here Counter.

Counter.cs

using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Windows;
using
Microsoft.SharePoint;
using
Microsoft.SharePoint.Administration;

namespace WindowsFormsApplication3
{
  public   class Counter :SPPersistedObject
    {
      [Persisted]
      public string  Password ;
      [Persisted]
      public string Name;
      public Counter()
      {
      }
      public Counter(String name, SPPersistedObject parent): base(name, parent)
      {
      }
    }

}

Explanation

  1. Extend the class SPPersistedObject in the custom class.

  2. Attribute all the variable to be serialized with [Persisted]

  3. Persisted attribute could only be put on variables. Not on properties

  4. Custom class must have default constructor.

Step 3: Writing data in Configuration data base

Counter
counter = new Counter("d3", server.Farm);
counter.Name = "dhananjaykumar";
counter.Password = "password";
idForFuture = counter.Id;

counter.Update();

Explanation

  1. Server.Farm is the farm on the SPServer. We need to create instance of SPServer by passing server name in constructor.

    SPServer  server = new SPServer("ServerName");
     

  2. Counter is custom class and it got constructor which takes two input. First is Name of the SPPersistedObject and second is the Farm.

  3. Counter.Id is returning Id of SPPersistedObject. We could use this ID in future for retrieval of this object from configuration data base.

  4. Counter.Update() is the method to update serialized data.

Step 4: Reading data from Configuration database

Counter
echo = (Counter)server.Farm.GetObject(idForFuture);
MessageBox
.Show(echo.Name + echo.Password );

Explanation

  1. SPFarm.Local.GetObject(GUID of Object) is used to fetch the persisted object.

  2. idForFuture is the GUID of the object.

Complete code

using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
Microsoft.SharePoint.Administration; 

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        SPServer server;
        Guid idForFuture;
        public Form1()
        {
            InitializeComponent();
            server = new SPServer("servername");
        }
        private void btnWrite_Click(object sender, EventArgs e)
        {
            Counter counter = new Counter("d3", server.Farm);
            counter.Name = "dhananjaykumar";
            counter.Password = "password";
            idForFuture = counter.Id;
            counter.Update();
            MessageBox.Show("Data  Persisted");
        }
        private void btnRead_Click(object sender, EventArgs e)
        {
            Counter echo = (Counter)server.Farm.GetObject(idForFuture);
            MessageBox.Show(echo.Name + echo.Password );
        }
    }
}

Conclusion

I explained about SPPersistedObject class and its uses with an example. Please download the attachment code for better understanding. Thanks for reading.

Up Next
    Ebook Download
    View all
    Learn
    View all