Click here to Skip to main content
Click here to Skip to main content

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

, 3 Jun 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Sohel_Rana
Architect ImpleVista Aps
Denmark Denmark
Sohel has more than six years of experience in professional software development with extensive involvement in Web based Object-Oriented, Multi-Tiered application design and development. He's Familiar with Test Driven Development (TDD) and refactoring techniques as well as having expertise in architecturing large enterprise applications. He has Experience in working with Content Management System and Portal Management System tools like SharePoint, DotNetNuke, Ektron.
 
Over last few years, he’s involved in development with projects on Microsoft SharePoint and received Microsoft MVP for SharePoint Server Development in the year 2011 and 2012. Currently he's working in a software company located Copenhagen,Denmark on a project integrating SharePoint and SAP. You can read his popular blog at: http://ranaictiu-technicalblog.blogspot.com

Comments and Discussions

 
GeneralAccess denied when user accessed is not SHAREPOINT\SYSTEM account PinmemberDanny Liemena16-Mar-10 23:12 
GeneralRe: Access denied when user accessed is not SHAREPOINT\SYSTEM account PinmemberSohel_Rana17-Mar-10 4:49 
QuestionWhy not use the built-in control itself? Pinmemberchiccodoro12-Jan-10 7:24 
AnswerRe: Why not use the built-in control itself? PinmemberSohel_Rana12-Jan-10 17:49 
GeneralMulti User serach PinmemberSuresh Kumar.A20-Dec-09 23:27 
GeneralRe: Multi User serach PinmemberSohel_Rana21-Dec-09 1:14 
GeneralThe browse window Pinmemberexplic_it15-Dec-09 3:05 
GeneralThe browse search Pinmemberexplic_it7-Dec-09 20:39 
GeneralRe: The browse search Pinmemberexplic_it7-Dec-09 22:08 
GeneralCheck this Site to add People Picker Programmatically [modified] PinmemberAnoosh Rahman29-Dec-08 5:00 
GeneralExcellent - Integrated With Web Part Nicely Pinmembernamaishi4-Nov-08 13:35 
GeneralGreat find.. Pinmemberwgyger28-Jun-08 9:54 
GeneralWhy screen shots PinmvpJohn Simmons / outlaw programmer4-Jun-08 1:22 
GeneralRe: Why screen shots PinmemberSohel_Rana4-Jun-08 2:59 
GeneralRe: Why screen shots PinprotectorMarc Clifton4-Jun-08 3:27 
GeneralRe: Why screen shots PinmemberSohel_Rana4-Jun-08 18:40 
GeneralRe: Why screen shots PinmvpJohn Simmons / outlaw programmer5-Jun-08 0:30 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 4 Jun 2008
Article Copyright 2008 by Sohel_Rana
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid