Sitecore Swamp

Dive in the Sitecore Swamp

NAVIGATION - SEARCH

Sitecore Virtual User - Can't Save Profile Property

It's quite common to create some custom properties and save them for virtual users. I just encountered a problem that the profile properties are not saved, even specifically calling virtualUser.Profile.Save().

After a couple hours code debugging and dot peeking, found the cause of the problem: 

The custom profile properties are saved into Core db - ClientData table (since Sitecore 8.1.1). The identifier is the session key. In Sitecore code, it's using WebUtil.GetSessionID() to generate the key. Here is the peeked code:

    public static string GetSessionID()
    {
      HttpContext current = HttpContext.Current;
      if (current != null && current.Session != null)
      {
        if (current.Session.IsNewSession && Settings.Performance.ForceASPSessionIDPersist)
          current.Session["__forceSessionPersist"] = (object) "dummy";
        return current.Session.SessionID;
      }
      string sessionIdCookieName = WebUtil.GetSessionIdCookieName();
      Assert.IsNotNull((object) sessionIdCookieName, "key");
      if (!Context.IsUnitTesting)
        return WebUtil.GetCookieValue(sessionIdCookieName);
      return WebUtil.GetCookieValue(sessionIdCookieName, "unittests.net_sessionid");
    }

If this is the initial request, the session can be null, and, of course, there no session cookie either. In this case, WebUtil.GetSession() returns empty string, which causes nothing can be persisted into database.

Add comment