JoelBlogs - Joel Jeffery's Microsoft 365 Blog

Microsoft 365, SharePoint, Teams and Office 365 Architecture, Development, Administration and Training

  • Home
    • Sitemap
  • Articles
    • #SPThingADay
    • SharePoint Online
      • SharePoint Online – Drag and Drop and Large File Uploads
    • SharePoint 2016
    • SharePoint 2013
      • Content Database Changes to the AllUserData Table
    • SharePoint 2010
      • Administration
        • Disable CRL Checking
        • Excel 2010 & PowerPivot
        • Limits & Thresholds
        • PeoplePicker AD Errors
        • Recycle Bin Behaviour
        • Renaming a Server
        • Service Pack 1
        • Unattended Installs
        • Uninstall All SharePoint 2010 Solutions via PowerShell
        • User Alert Management
        • Virtualised SharePoint
        • Visio Stencils for Administrators
      • Development
        • Audience Membership Workflow Activity
        • Base Types, Lists & Content Types
        • BCS & Offline Sync
        • Debugger Skipping Lines
        • Development Laptop Spec
        • Enabling JavaScript IntelliSense
        • Event Receivers & Deployment Jobs
        • FavIcons & SPUrl
        • Google Maps Sandbox Web Part
        • Group By Content Type for List Views
        • Locale Stapler / Master or Default Locale
        • Removing Default Editor Parts
        • Sandbox Embedding Resources
        • Solution Sandbox Introduction
        • SPPersistedObject
        • Restoring Deleted SPSites in SP1
        • SPWebConfigModification 1
        • SPWebConfigModification 2
        • STSADM copyappbincontent vs. Install-SPApplicationContent
        • Workflows for Beginners
        • Workflow InitiationData Seralizer
    • SharePoint 2007
      • Alternate Access Mappings
      • Excel Services
      • Excel Services UDFs & Excel Client 2007
      • Experiences from the Field
      • InfoPath & Forms Server
      • Kerberos & SSRS
      • Records Management
      • Web Application Service
      • WSS vs MOSS
  • Training
    • SharePoint Admin Links
  • Downloads
    • Summary Slides for PowerPoint
    • CodePlex Projects
      • Audience Membership Workflow Activity
      • Google Maps Sandbox Web Part
      • Group By Content Type in List Views
      • Locale Stapler / Master or Default Locale
      • SharePoint Outlook Connector
  • Hire Me!
    • MCP Transcript
    • Résumé/CV

SPPersistedObject: Persisting Configuration Data in SharePoint 2010

October 10, 2010 by Joel Jeffery

Continuing in my series of articles showing the many ways in which we can store configuration data for your SharePoint applications, this time we look at how to use SPPersistedObject to achieve this.

Don’t forget to check out part 1 of the series in which we find out how SPWebConfigModification can be used for persisting application configuration in SharePoint 2010.

Persistence Using SPPersistedObject

SPPersistedObjects let you store hierarchical configuration data for your application in SharePoint. An SPPersistedObject is a class you subclass. You can then add fields (not properties) to your class, attribute them with [Persisted], and SharePoint will serialise the object to Xml in the SharePoint Configuration database.

Code Snippet
  1. [GuidAttribute(“8AC7737A-8A57-4DD9-A782-B0B3F4595884”)]
  2. public class JoelsConfigSettings : SPPersistedObject
  3. {
  4.     public JoelsConfigSettings()
  5.     {
  6.     }
  7.     public JoelsConfigSettings(string name, SPPersistedObject parent)
  8.         : base(name, parent)
  9.     {
  10.     }
  11.     public JoelsConfigSettings(string name, SPPersistedObject parent, Guid id)
  12.         : base(name, parent, id)
  13.     {
  14.     }
  15.     [Persisted]
  16.     public string Value1;
  17.     [Persisted]
  18.     public int Value2;
  19. }

The above code creates a class with two fields (string Value1 and int Value2) which can be persisted for the current Web Application with the following code:

Code Snippet
  1. SPSite s = SPContext.Current.Site;
  2. JoelsConfigSettings settings = new JoelsConfigSettings(“JoelsSettingsKey”, s.WebApplication, s.ID);
  3. settings.Value1 = “Joels Value 1”;
  4. settings.Value2 = 2;
  5. settings.Update();

We can later retrieve – and optionally delete – the settings by calling GetChild<> on the WebApplication object.

Code Snippet
  1. SPSite s = SPContext.Current.Site;
  2. JoelsConfigSettings settings = s.WebApplication.GetChild<JoelsConfigSettings>(“JoelsSettingsKey”);
  3. string value1 = settings.Value1;
  4. int value2 = settings.Value2;
  5. // optionally delete our settings
  6. settings.Delete();
  7. // but proper removal requires unprovisioning and uncaching too
  8. settings.Unprovision();
  9. settings.Uncache();

As an alternative to storing at the Web Application level, you can use a Farm object instead.

Advantages

You configuration data is stored in the SharePoint Configuration database. This means it will be backed up along with the rest of your SharePoint data – assuming you perform SharePoint backups, of course. Smile

Your data is also safe from prying eyes as there is no UI to edit these settings.

Disadvantages

There is no built in user interface to manage SPPersistedObjects. This is both a good and a bad thing: you have to provide your own way of editing the values you wish to store. To read and write these values you also need to impersonate a user, such as a Farm Administrator, with read and write privileges on the Config database (SPSecurity.RunWithElevatedPriviliges() won’t help here!). There are some reports of SPPersistedObject use corrupting the Hierarchical Object Store; it’s unclear whether this is due to incorrect usage or something more flaky. Reading the comments, one of our readers has suggested this corruption happens for them if they use obfuscated assemblies. (Many thanks, Joe!)

Proceed with caution, and thoroughly test your code first on a virtual, scratch, SharePoint farm.

Filed Under: SharePoint Tagged With: Development, SharePoint 2010, SharePoint Architecture

Joel is a full-stack cloud architect who codes. He is a Microsoft Certified SharePoint Online, SharePoint Server and Azure specialist and Microsoft Certified Trainer.
He has over 20 years' experience with SharePoint and the Microsoft .NET Framework.
He's also co-founder of Microsoft Gold Partner JFDI Consulting Ltd. Read More…

Recent Posts

  • Microsoft Flow Tip #1 – Word Templates and Hiding Empty Repeating Sections
  • SharePoint PowerShell Tip #1 – Select-Object and FieldValues
  • Popular Misconceptions – Microsoft Teams relationship with SharePoint
  • Course: Microsoft 365 Certified Teamwork Administrator
  • Audience Targeted Searches in Modern SharePoint Online
MCT 2020-2021
Microsoft Teamwork Administrator Associate
Joel's Acclaim Profile
Joel's Microsoft Profile

Tags

Administration Architecture Certification Cloud Development Information Architecture intranets MCP Microsoft Microsoft Architecture Microsoft Azure migration Mobile Development MOSS MOSS 2007 office365 Office 365 Office 365 PowerShell SaaS SharePoint SharePoint 2010 SharePoint 2010 Training SharePoint 2013 SharePoint Administration SharePoint Administrator SharePoint Architecture SharePoint Designer 2010 SharePoint Developer SharePoint Development SharePoint Online sharepointonline SharePoint Search SharePoint Training SharePoint Videos Silverlight SOA Solution Sandbox SPThingADay TechEd 2007 Training Videos Visual Studio 2010 Windows Phone 7 WSS

Copyright © 2022 Joel Jeffery, SharePoint Architect