Tuesday, October 4, 2011

Adding the Full Name Property to User Lists

My earlier post about linking data to the LightSwitch user list has turned out to me my most popular yet. In fact, I've actually used this technique for the 2nd time in project and wound up using my own blog as a reference implementation! In my latest project I added feature that will probably prove useful to anyone using this technique: the addition of the Full Name property. This is a profile property that LightSwitch adds to users in the membership provider that is not part of the standard user properties.

Adding the Full Name property is pretty easy. First, of course, you have to add a FullName property to the Data Transfer Object (DTO):

public class User
    {
        [Key]
        public Guid UserId { get; set; }

        public string UserName { get; set; }

        public string FullName { get; set; }
    }

Next, we have to populate the FullName value from the profile properties. This part involves working with the ASP.Net Profile interface, which is more obscure that the main Membership interface:


var userList = Membership.GetAllUsers();

    foreach (MembershipUser user in userList)
    {
        var returnUser = new User 
        { 
            UserId = (Guid)user.ProviderUserKey, 
            UserName = user.UserName 
        };

        //create a profile object to query
        var profile = ProfileBase.Create(user.UserName);

        //look up the LightSwitch FullName property in the profile
        returnUser.FullName = profile.GetPropertyValue("FullName").ToString();

        returnValue.Add(returnUser);
    }

In order to understand how this code fits in with the overall service, please refer to my earlier post.

Adding the FullName property will allow you to display the full user name that you entered using the LightSwitch user manager, so you won't have to remember user names to identify users of your application.

1 comment:

jmanley said...

Can you also use this to add "Email" To the light switch user profile?