Shell profile
Describes how to use the profile from client-side code.
Optimizely 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.
Client API
The client API provides methods to read, write, and observe profile values from Dojo components.
Initialize the client profile object
Usually, the client profile is registered in the service locator, where multiple components can manipulate the same profile and observe changes on the profile. CMS already performs this initialization, so it is not required when creating CMS components.
initialize: function (settings) {
// Once during module initialization
//Load the epi/shell/Profile module
this.registerDependency("epi.shell.Profile", new epi.shell.Profile());
}Access and store the global profile object on a component
To work with the client profile, assign an object field during component initialization.
postCreate:function () {
// resolve profile from dependency system
this._profile = dependency.resolve("epi.shell.Profile");
},Read from the client profile object
To access values in the profile, use the get method.
getProfileValue: function() {
// get value from profile
return this._profile.get("MyKey");
},Write to the client profile object
To set values in the profile, use the set method.
setProfileValue: function(value) {
this._profile.set("MyKey", value);
}Observe changes to the client profile
The profile is a Dojo observable object. Use the Dojo API to observe changes.
postCreate: function () {
this._profile = dependency.resolve("epi.shell.Profile");
this._profile.watch("MyKey",
function (name, oldValue, value) {
this.doSomethingUsefulWith(value);
}
.bind(this)
);
},Server API
The server API lets you read, write, and set default values for profiles from server-side code.
Read profile data
To read profile values, use the profile repository.
using EPiServer.Shell.Profile;
public class ProfileManipulator: TemplatePage {
public object GetMyValueFromProfile() {
if (User.Identity.IsAuthenticated) {
var profile = _profileRepository.GetOrCreateProfile(User.Identity.Name);
object value;
if (profile.Settings.TryGetValue("MyKey", out value)) {
return value;
}
}
return null;
}
}Write profile data
To set profile values programmatically, get a profile using the profile repository, change it, and use the save method on the profile repository. The value must be a type supported by Optimizely Dynamic Data Store (DDS).
using EPiServer.Shell.Profile;
public class ProfileManipulator: TemplatePage {
public void StoreValueInProfile(bool isOrIsnt) {
if (User.Identity.IsAuthenticated) {
var profile = _profileRepository.GetOrCreateProfile(User.Identity.Name);
profile.Settings["MyKey"] = isOrIsnt;
_profileRepository.Save(profile);
}
}
}Give profiles default values
To set default values on a profile, listen to events on the profile repository and manipulate the profile object before it is used for the first time. An initializable module is an appropriate place for this.
using EPiServer.Shell.Profile;
[InitializableModule]
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;
}
}Updated 17 days ago
