Monday, July 27, 2009

Reading all users from people picker control

Scenario:
People picker control is great as it allows you to choose multiple users from authentication store. Also sometime we need to read all the users entered in people picker control.

Code:

protected PeopleEditor userPicker;

private SPUser[] ProcessUsers(SPWeb web)
{
PickerEntity entity;
if (!this.userPicker.Visible)
{
return new SPUser[] { web.CurrentUser };
}
int count = this.userPicker.ResolvedEntities.Count;
ArrayList list = new ArrayList();
SPUser[] userArray = new SPUser[count];
for (int i = 0; i < count; i++)
{
entity = (PickerEntity) this.userPicker.ResolvedEntities[i];
if (web.SiteUsers.GetByLoginNoThrow(entity.Key) == null)
{
list.Add(entity);
}
}
if (list.Count > 0)
{
SPUserInfo[] addUsersInfo = new SPUserInfo[list.Count];
for (int k = 0; k < list.Count; k++)
{
entity = (PickerEntity) list[k];
SPUserInfo info = new SPUserInfo();
info.LoginName = entity.Key;
info.Email = (string) entity.EntityData[PeopleEditor.PeopleInfo.Email];
info.Name = entity.DisplayText;
info.Notes = string.Empty;
addUsersInfo[k] = info;
}
web.SiteUsers.AddCollection(addUsersInfo);
}
for (int j = 0; j < count; j++)
{
entity = (PickerEntity) this.userPicker.ResolvedEntities[j];
userArray[j] = web.SiteUsers[entity.Key];
}
return userArray;
}

4 comments:

Anonymous,  July 28, 2009 at 5:45 AM  

Thanks a lot for your post.

Can you please explain me more how to deploy or make that code work.

Thanks in advance.

P/s: My email is tdkhoa3112@live.com. Very glad too see you.

Sandeep K Nahta July 28, 2009 at 8:12 AM  

You can use the code in many spot , so i didn't tied it to one.

One basic example can be that you have a people picker control on your Layout ASPX page, and the method will return you all the users in the people picker.

Other can be a WP, you need to dynamically add this control to ur WP and then read all entries using the function given.

Kanchana April 22, 2011 at 5:57 AM  

people picker control is placed on the grid. how i disable the People Picker control in run time?.

Sandeep K Nahta April 22, 2011 at 6:16 AM  

you can use some client side javascript to do that