In custom SharePoint web parts, it is often useful to use the people editor control to add, edit, and surface users stored within the SPFieldType.User list fields.When using an ascx control as the child control to the web part, this is simply created by first adding the following assembly registration to the top of the ascx file.
<%@ Register Assembly=”Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” Namespace=”Microsoft.SharePoint.WebControls” TagPrefix=”sp1″ %>
Then the controls may be added within the ascx control.
<sp1:PeopleEditor ID=”peUsers” runat=”server” MaximumEntities=”12″ MultiSelect=”true” AllowEmpty=”true” ErrorMessage=”List of contributors must not be more than 12 users” ValidatorEnabled=”true” Width=”200″ AllowTypeIn=”true” />
Note that this may also be created in the code behind by using PeopleEditor pe = new PeopleEditor();.
Now let’s assume that we would like to retrieve multiple users from
Retrieving SharePoint users from an SPFieldType.User with multiple users may be performed with the following method.
public static string GetMultipleUsers(SPWeb web, SPListItem splItem, string columnName)
{
string item = splItem[columnName].ToString();
if (string.IsNullOrEmpty(item))
{
return “”;
}
SPFieldUserValueCollection users = new SPFieldUserValueCollection(web, item);
string usersCommaDelimited = string.Empty;
foreach (SPFieldUserValue user in users)
{
if (usersCommaDelimited == string.Empty)
usersCommaDelimited = user.User.LoginName;
else
usersCommaDelimited += “,” + user.User.LoginName;
}
return usersCommaDelimited;
}
Now adding these users to the people editor is simple.
peUsers.CommaSeparatedAccounts = Utilities.Helpers.GetMultipleUsers(SPContext.Current.Web, someListItem, “Your User Column”);