Sitecore custom User Profile template

Recently there was a requirement to create Sitecore users programatically and  display custom properties in user profile.
User profile can be reached from User Manager > Edit Menu > Profile Tab



we went through few blog posts and came up with below solution

1.Update Sitecore User profile item with custom fields. (ideally should use new item inheriting the original User profile template. This helps during Sitecore upgrades)
a.Add the required fields to user template in core DB. Template Path in core database: /sitecore/templates/System/Security/User



2.Code snippet for creating the user programmatically.
a.Membershipconstants is our constants class
b.user is an internal User class object (used in below code – user.AccountName)

var membershipStatus = new System.Web.Security.MembershipCreateStatus();
            var domainUserName = MembershipConstants.SitecoreDomain + user.AccountName;
            System.Web.Security.Membership.CreateUser(domainUserName, MembershipConstants.InitialPassword, user.Email,
                MembershipConstants.SecurityQuestion,
                MembershipConstants.SecurityAnswer,
                true, out membershipStatus);
            if (membershipStatus == MembershipCreateStatus.Success)
            {
                Sitecore.Security.Accounts.User securityAccountUser = Sitecore.Security.Accounts.User.FromName(domainUserName, true);
                if (securityAccountUser != null)
                {
                    using (new SecurityDisabler())
                    {
                        Sitecore.Data.Database dbCore = Sitecore.Configuration.Factory.GetDatabase("core");
                        Sitecore.Data.Items.Item profileItem = dbCore.GetItem(@"/sitecore/system/Settings/Security/Profiles/User");
                        securityAccountUser.Profile.ProfileItemId = profileItem.ID.ToString();
                        securityAccountUser.Profile.Save();
                    }
                }
            }

3.Created user in Sitecore with custom fields.



4.Code to read a custom field.

Sitecore.Context.User.Profile.GetCustomProperty(MembershipConstants.RoleField)

5.Code to set a custom field for a user.
 var securityAccountUser = Sitecore.Security.Accounts.User.FromName(domainUserName, true);

            if (securityAccountUser != null)
            {
                try
                {
                    using (new SecurityDisabler())
                    {
                        var coreDatabase = Factory.GetDatabase(("core");
                        var profileItem = coreDatabase.GetItem((@"/sitecore/system/Settings/Security/Profiles/User");
                        securityAccountUser.Profile.ProfileItemId = profileItem.ID.ToString();
                        securityAccountUser.Profile.SetCustomProperty(
                            MembershipConstants.RoleField,
                            !string.IsNullOrWhiteSpace(user.RoleName) ? user.RoleName : string.Empty);
                        securityAccountUser.Profile.SetCustomProperty(
                            MembershipConstants.UserIdField,
                            user.Id.ToString());

                        securityAccountUser.Profile.Save();
                    }
                }
            }

Comments