Dave Burke : Freelance .NET Web Developer specializing in Online Communities

Creating user extended data on CS Admin Create New Account

Try as I might, I couldn't store new First and Last Name extended user data fields I added on the Create New Account page today.

 




The first and last name data is supposed to be serialized and stored as a key/value set in the PropertyNames and PropertyValues cs_userProfile fields.  As you can see from the screenshot below, no extended user data is going in those fields-in-waiting.

 

 

[Information about user profile, the web.config and asp.net membership in original post removed...  User Profile and User Extended Data are two different things.  I needed both to integrate with another asp.net app which is why I brought them into the discussion.  My bad.  Sorry.]


User extended attributes are added to the CommunityServer.config file.  Ours would look like this.


<ExtendedUserData>
    <add name="FirstName" />
    <add name="LastName" />
</ExtendedUserData>


They can then be added and managed in the CreateUserForm (sign-up), EditUserForm (profile edit), the UserAdd.aspx (CP Create User) and UserEdit.aspx (CP Edit User) pages.  Interestingly, all of these were working correctly except when I created a new account with UserAdd.aspx.

The ExtendedAttributes GetSerializerData() method called in the SqlCommonDataProvider's CreateUpdateDeleteUser() method is looking for this.ExtendedAttributes and none were being passed from the UserAdd.aspx.cs. 


public SerializerData GetSerializerData()
{
    SerializerData data = new SerializerData();

    string keys = null;
    string values = null;

    Serializer.ConvertFromNameValueCollection(this.extendedAttributes,ref keys, ref values);
    data.Keys = keys;
    data.Values = values;

    return data;  // DBVT - Always null since user object from UserAdd contains no extended attributes
}

 

The solution was to add the method used in other forms, SetExtendedUserData(user).  This populates the attributes and allows us to store user extended data in the cs_userprofile property fields.


// Determine if the account was created successfully
    switch (status)
    {
        // Everything went off fine, good
        case CreateUserStatus.Created:
            SaveAdditionalProfileData(user);
            // DBVT Modified
           SetExtendedUserData(user);

           Response.Redirect(SiteUrls.Instance().ControlPanelUserAddedMessage(user.UserID));
           break;
    }           


private void SetExtendedUserData(User user)
{
    foreach (string controlName in ExtendedUserData.Fields)
    {
    Control c = TaskRegion.FindControl(controlName);
    if (c != null)
    {
        string value = null;

        if (c is TextBox)
        value = ((TextBox)c).Text;
        else if (c is ListControl)
        value = ((ListControl)c).SelectedValue;
        else if (c is CheckBox)
        value = ((CheckBox)c).Checked.ToString();

        if (value != null)
        user.SetExtendedAttribute(c.ID, value);
    }
    }
    Users.UpdateUser(user);
}


Isn't the Source Available application model a beautiful thing?  That's all from the Community Server Adventure Channel today.  Check back for our next exciting episode soon!

[UPDATE:  This post was substantially updated 6/9 12:17 PM thanks to input from my friend José Lema.  By the light of a new day, I should also add that I'm not suggesting that this is a Community Server bug, since the new user form isn't handling any extended attributes.  But if you want to store user extended attributes in the UserAdd form, you'll need to add the SetExtendedUserData() method, which is the point of this post.]

Comments (2) | Post RSS RSS comment feed

Posted on 6/8/2007 4:26:28 PM by Dave Burke
Categories: .NET | Community Server
Tags: no tags for this post

Related posts

Comments (2) -

6/8/2007 8:56:12 PM Permalink

I'm a bit confused by what you're trying to do. The ExtendedUserData feature only works with fields set in the communityserver.config file, not with Profile fields set in the web.config file.

Did I misunderstand the error? Ping me when you're online so we can chat as I'd love to rectify a bug if indeed there is one.

José Lema |

6/9/2007 9:47:41 AM Permalink

Jose, thanks for calling me for the confusion.  You're absolutely right.  web.config->asp.net membership profile.  I needed both to share with another asp.net app which is why I included it.  I removed it to avoid further confusion.  User Profile data and ExtendedUserData are two separate things.  The distinction can be confusing (as I proved in my original post), so thank you for pointing that out.  A very good lesson.


The crux of it is that ExtendedAttributes.GetSerializerData() returns null since the user object passed to CreateUpdateDeleteUser() contains no extended attributes.


I rewrote the post thanks to your input, so the issue should hopefully be more clear.  If you re-read the post with the [Update] at the bottom you'll see I'm not saying that this is a bug in UserAdd.  Sorry to imply that in the original.


daveburke |


Powered by BlogEngine.NET 2.0.0.36
Theme by Dave Burke