Click here to Skip to main content
Licence CPOL
First Posted 3 Jun 2008
Views 33,895
Downloads 241
Bookmarked 21 times

Use SharePoint User or Group Picker in your WebParts/user controls

By | 3 Jun 2008 | Article
This article shows how you can use the SharePoint provided user/group picker in your WebPart/user control.

Introduction

SharePoint has a user and group picker which allow browsing SharePoint users and groups. Sometimes, we need to use the user group browsing facility in our custom WebPart or user controls. I have used Firefox Firebug to inspect the way the user group picker works. And, I have got the way it works! I think I need to share this with everyone.

Basics

The SharePoint provided user/group picker contains four controls as shown in figure 1 and described below:

  1. A textbox to enter user/group name.
  2. A check-name icon to check if the user/group name is valid.
  3. A browse icon to allow user/group to be searched.
  4. A hidden control which shows messages when user/group name not found.
  5. To show an error message when a user/group name is not found, the SharePoint built-in user/group picker uses a span whereas I have used a label.

Figure 1: User/Group picker

After user/group name is entered in the text box, there are two ways to check if the user/group name is valid. One way is to press Enter from the keyboard, another is to click on Check Names. I have implemented the user/group name checking logic in the Check Names icon (the ValidateSharePointUser function in the code-behind page). When the Enter key is pressed in the textbox, with a JavaScript, I actually call the Check Names icon’s click event in the JavaScript function fetchUserName().

When the browse icon is clicked, a new screen appears, allowing users/groups to search. The browse button in the SharePoint built-in User/Group Searcher is actually an anchor link (an <a href…….> in HTML). In the Click event of the anchor, a JavaScript function is called. In my example, the function is _Dialog_UserField_browse. I have tried to mimic the actual SharePoint code. In this function, I have found that SharePoint uses the current site name to show the picker. So, I have taken a hidden control in my page and put the current site name in the page_load event. The URL for the picker, as I have found, is something like: \u002fSiteName\u002f_layouts\u002fPicker.aspx?........;.

I have found the link is fixed except the SiteName, which is the current site name. So, I need to put the site name in the link. That’s why I put the site name in the hidden control. But remember that, in the case of top level site (i.e., SPSite) the site name is empty, so the \u002fSiteName will be removed from the link as I have done in _Dialog_UserField_browse. Finally, in the _Dialog_UserField_browse JavaScript function, a dialog is called, as commonShowModalDialog, with three parameters. The first parameter, sDialogUrl, is the location of the picker window. The second parameter, sFeatures, is the settings of the picker window (i.e., width, title etc.). The third parameter, CallbackWrapper_User_Browse, is the callback JavaScript function which is to be called when the user closes the picker window.

So, in the CallbackWrapper_User_Browse function, I need to parse the result of the picker and put the user name in the textbox. The picker will return an XML (if the user is found) like below:

<Entities Append=&quot;False&quot; Error=&quot;&quot; Separator=&quot;;&quot; MaxHeight=&quot;3&quot;>
<Entity Key=&quot;ONIRBAN\sohel&quot; DisplayText=&quot;ONIRBAN\sohel&quot; IsResolved=&quot;True
    Description=&quot;ONIRBAN\sohel&quot;>
<ExtraData>
<ArrayOfDictionaryEntry xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
    xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;>
<DictionaryEntry>
<Key xsi:type=&quot;xsd:string&quot;>DisplayName</Key>
<Value xsi:type=&quot;xsd:string&quot;>ONIRBAN\sohel</Value>
</DictionaryEntry>
<DictionaryEntry>
<Key xsi:type=&quot;xsd:string&quot;>SPUserID</Key>
<Value xsi:type=&quot;xsd:string&quot;>1</Value>
</DictionaryEntry>
<DictionaryEntry>
<Key xsi:type=&quot;xsd:string&quot;>PrincipalType</Key>
<Value xsi:type=&quot;xsd:string&quot;>User</Value>
</DictionaryEntry>
</ArrayOfDictionaryEntry>
</ExtraData>
<MultipleMatches />
</Entity>
</Entities>

I have used XML DOM to parse the return XML from the picker and put the username in the textbox.

When the user clicks on the Check Names icon, the server-side function ValidateSharePointUser is called, which checks if the user has permission to the site.

Using the Code

The project, along with this article, has a user control ctlUserGroupBrowser.ascx which has the user/group picker. To use the control, you need to use SmartPart from here. Download the SmartPart and install the web part. Then, compile the project and put the UserGroupBrowse.dll in the GAC. Now, add the following entry in the SafeControls section of the web.config:

<SafeControl Assembly=&quot;UserGroupBrowse, Version=1.0.0.0, Culture=neutral,
    PublicKeyToken=af26021c4978ee97&quot; Namespace=&quot;UserGroupBrowse&quot; 
    TypeName=&quot;*&quot; Safe=&quot;True&quot; />

Then, add the following entry in the <assemblies> section of the web.config:

<add assembly=&quot;UserGroupBrowse, Version=1.0.0.0, Culture=neutral,
    PublicKeyToken=af26021c4978ee97&quot; />

Put the ctlUserGroupBrowser.ascx in the usercontrols folder of the port of the SharePoint site’s virtual directories. Go to the site, and add the SmartPart web part to the page. Finally, browse the ctlUserGroupBrowser.ascx control from the SmartPart toolpane.

AJAX Extension

When you use the control, the page will be refreshed as you click Check Names or press Enter in the text box. To get rid of this, you need to use the AJAX supported SmartPart. You can get the SmartPart with AJAX support from here. Also, you need to wrap:

<div id=&quot;actionItemPanelDivision&quot;>

with:

<asp:UpdatePanel ID=&quot;ActionItemPanel&quot; runat="&quot;server&quot;">
<ContentTemplate>

Then, your control markup will be as shown below:

Figure2.JPG

Conclusion

I have tried to share my knowledge with all other SharePoint developers. Thanks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sohel_Rana

Web Developer

Bangladesh Bangladesh

Member

Sohel Rana is currently working as Software Engineer in a software firm in Dhaka, Bangladesh. He has years of expertise in working with products like SharePoint, Ektron, DotNetNuke. He's main expertise is in the area of SharePoint.
 
He likes to learn new technologies. He likes to listen music in his spare time.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAccess denied when user accessed is not SHAREPOINT\SYSTEM account PinmemberDanny Liemena22:12 16 Mar '10  
GeneralRe: Access denied when user accessed is not SHAREPOINT\SYSTEM account PinmemberSohel_Rana3:49 17 Mar '10  
QuestionWhy not use the built-in control itself? Pinmemberchiccodoro6:24 12 Jan '10  
AnswerRe: Why not use the built-in control itself? PinmemberSohel_Rana16:49 12 Jan '10  
GeneralMulti User serach PinmemberSuresh Kumar.A22:27 20 Dec '09  
GeneralRe: Multi User serach PinmemberSohel_Rana0:14 21 Dec '09  
GeneralThe browse window Pinmemberexplic_it2:05 15 Dec '09  
GeneralThe browse search Pinmemberexplic_it19:39 7 Dec '09  
GeneralRe: The browse search Pinmemberexplic_it21:08 7 Dec '09  
GeneralCheck this Site to add People Picker Programmatically [modified] PinmemberAnoosh Rahman4:00 29 Dec '08  
Add the Following in your HTML
 
<sharepoint:peopleeditor runat="server" id="pplEditor" xmlns:sharepoint="#unknown">
AutoPostBack="true" AllowEmpty="false" SelectionSet="User,SecGroup,SPGroup"
BorderWidth="1" Width="200px" PlaceButtonsUnderEntityEditor="false" Rows="1" />
 
that's all ..
 
Enjoy !
 
modified on Saturday, January 3, 2009 1:40 PM

GeneralExcellent - Integrated With Web Part Nicely Pinmembernamaishi12:35 4 Nov '08  
GeneralGreat find.. Pinmemberwgyger8:54 28 Jun '08  
GeneralWhy screen shots PinmvpJohn Simmons / outlaw programmer0:22 4 Jun '08  
GeneralRe: Why screen shots PinmemberSohel_Rana1:59 4 Jun '08  
GeneralRe: Why screen shots PinprotectorMarc Clifton2:27 4 Jun '08  
GeneralRe: Why screen shots PinmemberSohel_Rana17:40 4 Jun '08  
GeneralRe: Why screen shots PinmvpJohn Simmons / outlaw programmer23:30 4 Jun '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 4 Jun 2008
Article Copyright 2008 by Sohel_Rana
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid