🔧 Maintenance Alert: World will be on Read-Only Mode on February 18th, 10:00 PM – 11:00 PM EST / 7:00 PM – 8:00 PM PST / 4:00 AM – 5:00 AM CET (Feb 19). Browsing available, but log-ins and submissions will be disabled.

Stefan Forsberg
May 20, 2009
  8601
(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
The missing globe can finally be installed as a nuget package!

Do you feel like you're dying a little bit every time you need to click "Options" and then "View on Website"? Do you also miss the old "Globe" in...

Tomas Hensrud Gulla | Feb 14, 2025 | Syndicated blog

Cloudflare Edge Logs

Optimizely is introducing the ability to access Cloudflare's edge logs, which gives access to some information previously unavailable except throug...

Bob Davidson | Feb 14, 2025 | Syndicated blog

Comerce Connect calatog caching settings

A critical aspect of Commerce Connect is the caching mechanism for the product catalog, which enhances performance by reducing database load and...

K Khan | Feb 14, 2025

CMP DAM asset sync to Optimizely Graph self service

The CMP DAM integration in CMS introduced support for querying Optimizly Graph (EPiServer.Cms.WelcomeIntegration.Graph 2.0.0) for metadata such as...

Robert Svallin | Feb 13, 2025

PageCriteriaQueryService builder with Blazor and MudBlazor

This might be a stupid idea but my new years resolution was to do / test more stuff so here goes. This razor component allows users to build and...

Per Nergård (MVP) | Feb 10, 2025

Enhancing Optimizely CMS Multi-Site Architecture with Structured Isolation

The main challenge of building an Optimizely CMS website is to think about its multi site capabilities up front. Making adjustment after the fact c...

David Drouin-Prince | Feb 9, 2025 | Syndicated blog