How to add a PeopleEditor (people picker) as a webpart property

I can’t take credit for this work. I would never have been able to piece this together on my own. I got my code to work by reading: Add PeopleEditor (PeoplePicker) to Webpart Properties . I am documenting what I did in order to a) help others find a solution and b) have a copy for myself when I go to do this again later!  Winking smile

Step #1 is to have your webpart implement the IWebEditable interface. Then add the following code to the webpart:

   1: [Personalizable(PersonalizationScope.Shared)]   

   2:         [WebBrowsable(false)]   

   3:         public string Contacts   

   4:         {   

   5:             get  

   6:             {   

   7:                 return _Contacts;   

   8:             }   

   9:             set  

  10:             {   

  11:                 _Contacts = value;   

  12:             }   

  13:         }   

  14:   

  15: EditorPartCollection IWebEditable.CreateEditorParts()    

  16:         {    

  17:             List<EditorPart> editors = new List<EditorPart>();   

  18:             editors.Add(new PeoplePickerEditor(this.ID));   

  19:  

  20:             return new EditorPartCollection(editors);   

  21:         }   

  22:   

  23:         object IWebEditable.WebBrowsableObject    

  24:         {    

  25:             get { return this; }    

  26:         }  

Then you need to add a class to the project. Here is the class that I used:

   1: public class PeoplePickerEditor : EditorPart   

   2:   {   

   3:       private PeopleEditor _peoplePicker;    

   4:  

   5:       public PeoplePickerEditor(string webPartID)   

   6:       {   

   7:           this.ID = "PeoplePickerEditor" + webPartID;   

   8:           this.Title = "Select Contacts";   

   9:       }   

  10:  

  11:       protected override void CreateChildControls()   

  12:       {   

  13:           _peoplePicker = new PeopleEditor();   

  14:           _peoplePicker.ID = "pe1";   

  15:           _peoplePicker.AllowTypeIn = true;   

  16:           _peoplePicker.AllowEmpty = false;   

  17:           _peoplePicker.MultiSelect = true;   

  18:           _peoplePicker.Width = Unit.Pixel(250);   

  19:           _peoplePicker.SelectionSet = PeopleEditor.AccountType.User.ToString();   

  20:  

  21:           Controls.Add(_peoplePicker);   

  22:       }   

  23:       public override bool ApplyChanges()   

  24:       {   

  25:           EnsureChildControls();   

  26:           ContactsWebPart webPart = WebPartToEdit as ContactsWebPart;    

  27:           if (webPart != null)    

  28:           {   

  29:               webPart.Contacts = _peoplePicker.CommaSeparatedAccounts;   

  30:               //PickerEntity entity    

  31:           }    

  32:           return true;   

  33:       }   

  34:       public override void SyncChanges()    

  35:       {    

  36:           EnsureChildControls();   

  37:           ContactsWebPart webPart = WebPartToEdit as ContactsWebPart;    

  38:           if (webPart != null)    

  39:           {   

  40:               _peoplePicker.CommaSeparatedAccounts = webPart.Contacts;   

  41:               //_peoplePicker.ResolvedEntities = webPart.Contacts;   

  42:           }   

  43:       }   

  44:   }   

That’s all there is to it. Good luck! Ninja

Advertisements

About lelandholmquest

After serving in the Navy as a Reactor Operator on fast attack submarines, I earned both a Bachelor's and Masters in Information Technology from American InterContinental University and am currently working on my doctorate. I have a beautiful wife and two of the sweetest daughters a man could ask for. And I work for the greatest company: Microsoft. At Microsoft I work on the knowledge management system for Microsoft Services: Campus on Office 365.
This entry was posted in C# Programming, SharePoint Coding, Web Parts and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s