Skip Ribbon Commands
Skip to main content

Robin | zevenseas | SharePoint Blog

:

The zevenseas Community > Blogs > Robin | zevenseas | SharePoint Blog > Posts > Using the PeoplePicker in your custom webpart
June 17
Using the PeoplePicker in your custom webpart

In my current project I wanted to create a webpart where I could add users to a certain group in a site from a single adminstration site. Furthermore I wanted to use the nice and elegant People Picker. This control is called “PeopleEditor” which belongs to the Microsoft.SharePoint.WebControls namespace. This is how it looks like when ran in a webpart :

peoplepicker

Here is the code to get the values from the control :

PeopleEditor _peoplePicker = new PeopleEditor();
_peoplePicker.AllowTypeIn = true;
_peoplePicker.AllowEmpty = false;
_peoplePicker.MultiSelect = false;

SPWeb web = new SPSite("urlToWeb").OpenWeb();
SPGroup group = web.SiteGroups["groupname"];

web.AllowUnsafeUpdates = true;

if (Group.Users.Count > 0)
{
      SPUserCollection users = group.Users;
      foreach (SPUser user in users)
      {
           group.Users.Remove(user.LoginName);
           group.Update();
      }
}
   
            
try
{
      group.AddUser(_peoplePicker.Accounts[0].ToString(), "", "", "");
      group.Update();
}

catch (ArgumentException error)
{
      //Do ErrorHandling here
}

finally
{
     web.AllowUnsafeUpdates = false;
     web.Close();
     web.Dispose();
}

 

And here is the code to set the values from the control :

if (group.Users.Count > 0)
{                
      foreach (SPUser user in group.Users)
      {                   
             _peoplePicker.CommaSeparatedAccounts= user.LoginName;                
      }                
}

Took me a couple of hours to find out I had to use the CommaSeperatedAccount property. Since you are getting the values using the Accounts or the Entities properties, it seems a bit odd to use this method to set the values.

Comments

Yaroslav

Ya, but when you're assigning your login names to CommaSeparatedAccounts ... each new login overwiters existing one. So you end up with the last username in group instead of having all of the users assigned to a peopleeditor
Robin MeureNo presence information on 16/11/2008 07:41

Gavin Pollock

Hi Robin, I'm thinking about setting up a webpart that exposes a custom Property that allows you to select multiple users.

Any idea on how to approach this. Somehow I want to add a custom property that uses the peoplepicker like the Contact Details webpart does. (except for multiple contacts)
Robin MeureNo presence information on 16/11/2008 07:41

Richard

_peoplePicker.CommaSeparatedAccounts= user.LoginName;  

should be

_peoplePicker.CommaSeparatedAccounts += user.LoginName;  
System Account on 13/02/2009 11:55

Richard

oops. Don't forget the comma seperating them each.
_peoplePicker.CommaSeparatedAccounts += user.LoginName + ",";  
System Account on 13/02/2009 11:56

almir

Hi,

do you know if there´s a way to define the sort order of people picker dialog (I mean the shrepoint default picker dialog) ?

There ara some properties in stsadm, but no one to sort.

Thanks in advanced.
System Account on 19/10/2009 11:59

Shakti

Hi,

I am facing one problem...

I want to add the columns that are shown in the dialog of the People Picker. How can i control that. When i do PeoplePicker.ResolvedEntities[0].EntityData...where is this data coming from...?

Is it coming from Active Directory...?

If Yes, then how can i make sure that "Email" will always be the part of the EntityData collection...

thanks,
Shakti
System Account on 29/03/2010 13:01

dhib

I have the same issue. Please If you find the solution can you post it?
I can't sort the dialog of the People Picker.
System Account on 27/04/2010 06:08
 

 Statistics

 
Views: 3544
Comments: 7
Tags:
Published:1279 Days Ago