Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Episerver Shell contains a profile API store for storing editor settings; similar to the ASP.NET profile but more practical to use and has a client API available for Dojo components.
This section shows how to use the profile from client-side code.
Usually, the client profile is registered in the service locator where multiple components can manipulate the same profile and observe changes on the profile. The CMS application already performs this initialization so it you do not have to when you create CMS components.
initialize: function (settings) {
// Once during module initialization
dojo.require("epi.shell.Profile");
this.registerDependency("epi.shell.Profile", new epi.shell.Profile());
}
To work with the client profile, assign an object field during component initialization.
postCreate:function () {
// resolve profile from dependency system
this._profile = epi.dependency.resolve("epi.shell.Profile");
},
To access values in the profile, use the get method.
getProfileValue: function(){
// get value from profile
return this._profile.get("MyKey");
},
To set values in the profile, use the set method.
setProfileValue: function(value) {
this._profile.set("MyKey", value);
}
The profile is a Dojo observable object and you can use the Dojo API for observing changes.
postCreate:function () {
this._profile = epi.dependency.resolve("epi.shell.Profile");
this._profile.watch("MyKey", dojo.hitch(this, function (name, oldValue, value) {
this.doSomethingUsefulWith(value);
}));
},
This section shows how to use the Shell profile from server-side code.
To read profile values, use the profile repository.
using EPiServer.Shell.Profile;
public class ProfileManipulator : TemplatePage
{
public object GetMyValueFromProfile()
{
if(User.Identity.IsAuthenticated)
{
var repository = Locate.ProfileRepository();
var profile = repository.GetOrCreate(User.Identity.Name);
object value;
if (profile.Settings.TryGetValue("MyKey", out value))
{
return value;
}
}
return null;
}
}
To set profile values programmatically, get a profile using the profile repository, change it, and use the save method on the profile repository. Keep in mind that the value must be a type supported by DDS.
using EPiServer.Shell.Profile;
public class ProfileManipulator : TemplatePage
{
public void StoreValueInProfile(bool isOrIsnt)
{
if(User.Identity.IsAuthenticated)
{
var repository = Locate.ProfileRepository();
var profile = repository.GetOrCreate(User.Identity.Name);
profile.Settings["MyKey"] = isOrIsnt;
repository.Save(profile);
}
}
}
Setting values on a profile involves listening to events on the profile repository, and manipulating the profile object before it is used for the first time. A good place for doing this is in an initializable module.
using EPiServer.Shell.Profile;
public class ProfileInitializer : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
var repository = context.Locate.ProfileRepository();
repository.ProfileCreated += OnProfileCreated;
}
void OnProfileCreated(object sender, ProfileEventArgs e)
{
// Set setting to a value on newly created profiles
e.Profile.Settings["MyKey"] = true;
}
}
Last updated: Sep 21, 2015