zevenseas


 

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.

Links to this post

Comments

On 16 Nov 2008 04:41, 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

On 16 Nov 2008 04: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)

On 13 Feb 2009 08:55, Richard

_peoplePicker.CommaSeparatedAccounts= user.LoginName;

should be

_peoplePicker.CommaSeparatedAccounts += user.LoginName;

On 13 Feb 2009 08:56, Richard

oops. Don't forget the comma seperating them each.
_peoplePicker.CommaSeparatedAccounts += user.LoginName + ",";

On 19 Oct 2009 08:59, 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.

Name

Url

Email

Comments

CAPTCHA Image Validation



 
 
 

© 2009 Community Kit For SharePoint