Search code examples
sitecoresitecore8

Sitecore | Unable to set extranet\anonymous user details through code behind


We have a subscription form with 3 fields(Email, Phone number, Interests) displayed for anonymous users. When we click subscribe we need to save those details to extranet\anonymous users.

Form Looks like Subscription Form

Below is the code I am using:

protected void btnGo_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        try
        {
            var userProfile = Sitecore.Context.User.Profile;
            userProfile.FullName = "Anonymous";
            userProfile.ProfileItemId = "{C7E512C3-D10D-4EE7-A9D1-52474E858456}";
            userProfile.Email = txtEmail.Text;
            userProfile.SetCustomProperty("Phone Number", txtPhone.Text);
            userProfile.SetCustomProperty("Interests", ddlInterests.SelectedItem.Text);
            userProfile.Save();
        }
        catch (System.Web.Security.MembershipCreateUserException)
        {
            lblMessage.Text = GetDictionaryText("Unable to register");
        }
    }
}

I am getting An exception of type 'System.Configuration.Provider.ProviderException' occurred in Sitecore.Kernel.dll but was not handled in user code.

Please see below screen shot for exception:

Exception while assigning values/profile id to extranet\anonymous user

Can someone please suggest what could be the issue and how to assign details to anonymous user?


Solution

  • Assigning this information to the anonymous user is a bad idea because the user is shared over all anonymous sessions. Unless you are using actual users (or maybe virtual users), the good way to do this is to use the Sitecore Contact. More information can be found here (and related pages).

    It comes down to using Tracker.Current.Contact instead of Sitecore.Context.User.