Stefan Forsberg
May 20, 2009
  8427
(3 votes)

Adding a connected Company field to an EPiServer user – A love story

In the future posts about page provider I’ll add oil to the internal/private/coupled EPiServer classes fire started by Anders Hattestad. So before all that whining I wanted to share when implementing something in EPiServer is actually much smoother than I imagined. So suck up mode on!

Profile

What I want is to somehow connect a company name with an EPiServer user. My initial instinct was to use the asp.net functionality profiles (which EPiServer uses too to store for example the edit tree settings for a user) and then somehow enable the administrators of the site to administrate this on the regular user settings page.

So I looked at the profile section in web.config and it seems a Company setting was already in place for me to use:

<profile enabled="true" defaultProvider="SqlProfile" automaticSaveEnabled="true">
    <properties>
        <add name="Address" type="System.String" provider="SqlProfile"/>
        <add name="ZipCode" type="System.String" provider="SqlProfile"/>
        <add name="Locality" type="System.String" provider="SqlProfile"/>
        <add name="Email" type="System.String" provider="SqlProfile"/>
        <add name="FirstName" type="System.String" provider="SqlProfile"/>
        <add name="LastName" type="System.String" provider="SqlProfile"/>
        <add name="Language" type="System.String" provider="SqlProfile"/>
        <add name="Country" type="System.String" provider="SqlProfile"/>
        <add name="Company" type="System.String" provider="SqlProfile"/>
        <add name="Title" type="System.String" provider="SqlProfile"/>
        <add name="SubscriptionInfo" type="EPiServer.Personalization.SubscriptionInfo, EPiServer" provider="SqlProfile"/>
        <add name="CustomExplorerTreePanel" type="System.String" provider="SqlProfile"/>
        <add name="FileManagerFavourites" type="System.Collections.Generic.List`1[System.String]" provider="SqlProfile"/>
        <add name="EditTreeSettings" type="EPiServer.Personalization.GuiSettings, EPiServer" provider="SqlProfile"/>
        <add name="ClientToolsActivationKey" type="System.String" provider="SqlProfile"/>
        <add name="FrameworkName" type="System.String" provider="SqlProfile"/>
    </properties>
    <providers>
        <clear/>
        <add name="SqlProfile" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="EPiServerDB" applicationName="EPiServerSample"/>
    </providers>
</profile>

Plugin control

To enable the end users to administrate this setting we’ll add a control to the area called SidSettingsArea (which is the user settings area). Then I implemented the interface IUserSettings which contains the two methods

public void LoadSettings(string userName, EPiServer.Personalization.EPiServerProfile data)
{
    
}
 
public void SaveSettings(string userName, EPiServer.Personalization.EPiServerProfile data)
{
 
}

So luckily enough for me the profile data of the user is sent to these two methods which makes it easy to implement the functionality we want. Since company is one of the “built in” (sort to speak) profile values it’s available strongly typed in the EPiServerProfile class. So I added a textbox to my control (called tbCompany) where the user entered the company and bang, everything just worked. This is the complete code for the control:

[GuiPlugIn(DisplayName = "User Settings", Description = "Additional settings for the user", Area = PlugInArea.SidSettingsArea, Url = "~/UI/PlugIns/UserSettings.ascx")]
    public partial class UserSettings : System.Web.UI.UserControl, IUserSettings, ICustomPlugInLoader
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        #region IUserSettings Members
 
        public void LoadSettings(string userName, EPiServer.Personalization.EPiServerProfile data)
        {
            if(!IsPostBack)
            {
                tbCompany.Text = data.Company;
            }
            
        }
 
        private bool saveRequiresUIReload;
        public bool SaveRequiresUIReload
        {
            get
            {
                return saveRequiresUIReload;
            }
            set
            {
                saveRequiresUIReload = value;
            }
        }
 
        public void SaveSettings(string userName, EPiServer.Personalization.EPiServerProfile data)
        {
            data.Company = tbCompany.Text;
            data.Save();
        }
 
        #endregion
 
        #region ICustomPlugInLoader Members
 
        public PlugInDescriptor[] List()
        {
            return new PlugInDescriptor[] { PlugInDescriptor.Load(typeof(UserSettings)) };
        }
 
        #endregion
    }

user2

Accessing the data

To access the data on the site we use the class EPiServer.Personalization.EPiServerProfile. It contains a a static property called Current that’s mapped to the current users profile. So to access the company of the currently logged in user we’d simply write

EPiServer.Personalization.EPiServerProfile.Current.Company

If you add additional profile properties (that aren’t mapped in the EPiServerProfile class) you can access those bracket style (in the same manner as PageData)

EPiServer.Personalization.EPiServerProfile.Current["SomeProperty"]
May 20, 2009

Comments

Mark Stott
Mark Stott Aug 2, 2023 10:00 AM

Somewhat late to this article ... But this was just what I needed for a CMS 11 client.  Will be interesting to see if and how this is possible for CMS 12.

Please login to comment.
Latest blogs
Creating an Optimizely CMS Addon - Adding an Editor Interface Gadget

In   Part One   of this series, I covered getting started with creating your own AddOn for Optimizely CMS 12. This covered what I consider to be an...

Mark Stott | Aug 30, 2024

Configure your own Search & Navigation timeouts

The main blog Configure your own Search & Navigation timeouts was posted for years but you need copy the code to your application. We now bring tho...

Manh Nguyen | Aug 30, 2024

Joining the Optimizely MVP Program

Andy Blyth has been honoured as an Optimizely MVP, recognising his contributions to the Optimizely community. Learn how this achievement will enhan...

Andy Blyth | Aug 29, 2024 | Syndicated blog

Welcome 2024 Summer OMVPs

Hello, Optimizely community! We are thrilled to announce and welcome the newest members to the Optimizely Most Valuable Professionals (OMVP) progra...

Patrick Lam | Aug 29, 2024

Create custom folder type in Edit Mode

Content Folders, which are located in assets pane gadget or multichannel content gadget, allow you to add any type of block or folders. But...

Grzegorz Wiecheć | Aug 28, 2024 | Syndicated blog

Creating an Optimizely AddOn - Getting Started

When Optimizely CMS 12 was launched in the summer of 2021, I created my first AddOn for Optimizely CMS and talked about some of the lessons I learn...

Mark Stott | Aug 28, 2024