Tuesday, January 29, 2013

Disable (Read-only) for sharepoint 2010 People Picker (PeopleEditor)

Using the SharePoint 2010 People Picker (People Editor) user control in your custom visual web part is very powerful tool (which by the way makes you web part looks nice "JUST KIDDING IT IS SHIT" ) .

Any way .... you may be depressed and forced by the business needs to use this shit control, also by business needs you may want to disable this control programmatically to and use it only for display "Read Only". What we have learned in the ASP.NET is that any web control should have the property "Enabled" which if you ever set it to false the control is not enabled "LOGIC" , but hell no , The people picker do not offer you the easy way.
So lets have the following scenario, You want to auto fill this control in the page load with a specific user then disable it.

Assuming you have the following people editor control on your page or web part
 <SharePoint:PeopleEditor ID="PeoplePickerOwner" runat="server" Width="350" SelectionSet="User" MultiSelect="false" AllowEmpty="true" ValidationGroup="AdvancedSearchVld" />  

To auto fill and disable this control use the following method



   1:  private void SetOwnerUser()
   2:          {
   3:              if (CurrentUser != null)
   4:              {
   5:                  PeoplePickerOwner.Entities.Clear();
   6:                  PickerEntity UserEntity = new PickerEntity();
   7:                  UserEntity.DisplayText = CurrentUser.Name;
   8:                  UserEntity.Key = CurrentUser.LoginName;
   9:                  PeoplePickerOwner.Entities.Add(PeoplePickerOwner.ValidateEntity(UserEntity));
  10:                  PeoplePickerOwner.ShowButtons = false;
  11:                  PeoplePickerOwner.AllowTypeIn = false;
  12:              }
  13:          }




UPDATE: Danial Martins asked "I select the name press backspace it deletes it. Is there a workaround?", Thanks to Hoshang Akeryi, Below is the solution:




1
2
3
4
5
6
7
8
9
ppl.ShowButtons = false;
ppl.AllowTypeIn = false;
than add the script to your page:
<script type="text/javascript">    
   $(document).ready(function () 
   {
        $(this).find("div[Title='People Picker']").attr("contentEditable",false);
   }
</script>



2 comments:

  1. Thank you it worked :)

    ReplyDelete
  2. In my situation using your solution, if I select the name press backspace it deletes it. Is there a workaround?

    Thanks!

    ReplyDelete