Show TOC

Direct Mode: Simple Use CaseLocate this document in the navigation structure

In this example, the personalization data is a set of flags set by the user and then explicitly saved.

Implementation Steps

We need the following steps in our application:

  1. Application initialization:

    1. Get the personalizer.

    2. Read the personalization data.
  2. React on the Save button.

    1. Write the personalization data.

Get the Personalizer

There is one personalizer per item. Technically, each item is part of a container. So a personalizer is identified by the container and the item.

Sample Code
oPersonalizationService = sap.ushell.Container.getService("Personalization");
oComponent = sap.ui.core.Component.getOwnerComponentFor(this.getView());
oScope = {
  keyCategory : oPersonalizationService.constants.keyCategory.FIXED_KEY,
  writeFrequency: oPersonalizationService.constants.writeFrequency.LOW,
  clientStorageAllowed : true
};
...
oPersId = {
  container : "sap.ushell.samples.persSample1",
  item : "fruits"
};

...
oPersonalizer = oPersonalizationService.getPersonalizer(oPersId, oScope, oComponent);
Read the Personalization Data

The getPersData method is asynchronous and uses the deferred object functionality (jQuery.Deferred).

Sample Code
oReadPromise = oPersonalizer.getPersData()
  .done(function(oPersData){
    // Pseudo code: set the checkboxes on the UI according to the read personalization data
  })
  .fail(function() {
    jQuery.sap.log.error(“Reading personalization data failed.”)
  });
Write the Personalization Data

The save method is asynchronous as well.

Sample Code
oSavePromise = this.oPersonalizer.setPersData(oPersData)
  .done(function(){
    // Tell the user that the data was saved
  })
  .fail(function() {
    jQuery.sap.log.error(“Writing personalization data failed.”)
  });

The personalization service ensures that the save and load operations to the front-end server are sequentialized, so that a subsequent operation is only started when the previous one is finished.