SPPersistedObject example

 

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

namespace SPPersistedObject_Test
{
    public class Counter : SPPersistedObject
    {
        [Persisted]
        private int number = 99;     
        public int Number {
            get {
                return number;
            }
            set {
                number = value;
            }
        }
        public Counter()
        {
        }

        // ----------------------------------------------------------------------
        // Parameterized constructor is called by WSS runtime to initialize the class
        // ----------------------------------------------------------------------
        public Counter(string name, SPPersistedObject parent, Guid guid)
            : base(name, parent, guid)
        {         } // End of Counter constructor

    } // End of Counter class
}

 

static void Main(string[] args)
       {
           // Designate a Guid value that uniquely represents my Counter object...
           Guid storeGuid = new Guid("{3C5C252B-6D11-4bc5-8DE6-E9D292D49BB5}");

           // Designate which server will be utilized...
           SPServer server = new SPServer("pctrungpham");

           // Create a Counter object that is a child of the server farm...

           // ----------------------------------------------------------------------
           // Notes:
           // 1 - the object can be parented against any other SPPersistedObject, in this example the object is child of the farm
           // 2 - the object must have a unique, non-changing Guid that identifies the object within the entire scope of WSS
           // ----------------------------------------------------------------------
           Counter counter = new Counter("My Counter", server.Farm, storeGuid);

           // Update the field to a non-default value of 99...
           counter.Number = 10000;

           // Saves the object and push it out to all servers...
           counter.Update();

           // Now use a new object to prove the value was stored on the server...
           Counter echo = (Counter)server.Farm.GetObject(storeGuid);

           // Verify the object was stored with a non-default value...
           System.Diagnostics.Debug.WriteLine("This counter value should be 10,000: " + (echo.Number == 10000));

           // Now delete the object...
           echo.Delete();

           // And perform cleanup...
           echo.Unprovision();
       }

Comments

Popular posts from this blog

Setup SharePoint 2010

Register CSS to SP Master Page