Introduction of SPPersistedObject class in SharePoint

No.of Views1772
Bookmarked1 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : SharePoint , Development and Programming
Introduction of SPPersistedObject class in SharePoint
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

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");

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 data base

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.

 

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top