In a previous post I introduced the Automatic Portal Connection tool, which can be used to automatically setup the portal connection on new and existing sites.
I thought I would take a peek at how I've made this happen:
Firstly I'd like to introduce the SPPersistedObject:
The description MSDN uses is:
The SPPersistedObject class provides a base class for all administration objects. It serializes all fields marked with the Persisted attribute to XML and writes the XML blob to the configuration database. The SPPersistedObject class contains code to serialize all its members that are base types, other persisted objects, and collections of persisted objects. Configuration data that is stored in persisted objects is automatically made available to every process on every server in the farm.
The Portal Connection tool has a class called PortalConfig which derives from SPPersistedObject this class holds the configuration information such as the portal name and portal URL:
/// <summary> /// Simple persisted object that stores the portal connection name and the portal connection url /// </summary> public class PortalConfig : SPPersistedObject { [Persisted] private string portalName = string.Empty; public string PortalName { get { return portalName; } set { portalName = value; } } [Persisted] private string portalUrl = string.Empty; public string PortalUrl { get { return portalUrl; } set { portalUrl = value; } } public PortalConfig() { } public PortalConfig(string name, SPPersistedObject parent, Guid guid) : base(name, parent, guid) { } }
SPPersistedObject is a very handy class, especially for configuration type scenario that is used in this solution, a method that makes use of this object is:
/// <summary> /// Simple method to persist our settings to the site /// </summary> /// <param name="curWeb"></param> /// <param name="curSite"></param> public static void ApplyPortalConnectionSettings(SPWeb curWeb, SPSite curSite) { PortalConfig portalConfig = (PortalConfig)curSite.WebApplication.Farm.GetObject( new Guid(curSite.WebApplication.Properties[PROP_KEY].ToString()) ); if (curWeb.IsRootWeb) { curSite.PortalName = portalConfig.PortalName; curSite.PortalUrl = portalConfig.PortalUrl; } curWeb.Update(); }
This object is filled out and persisted by the central admin page, then a feature staple gets this information when the site is created and applies it, but I'll expand on this in future posts.