Click here to Skip to main content
15,884,099 members
Articles / Web Development / ASP.NET
Article

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

Rate me:
Please Sign up or sign in to vote.
3.50/5 (4 votes)
3 Jun 2008CPOL4 min read 73.1K   478   22   17
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.

Image 1

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:

XML
<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:

XML
<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:

XML
<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:

ASP.NET
<div id=&quot;actionItemPanelDivision&quot;>

with:

ASP.NET
<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)


Written By
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 Pin
Danny Liemena16-Mar-10 22:12
Danny Liemena16-Mar-10 22:12 
GeneralRe: Access denied when user accessed is not SHAREPOINT\SYSTEM account Pin
Sohel_Rana17-Mar-10 3:49
Sohel_Rana17-Mar-10 3:49 
QuestionWhy not use the built-in control itself? Pin
chiccodoro12-Jan-10 6:24
chiccodoro12-Jan-10 6:24 
AnswerRe: Why not use the built-in control itself? Pin
Sohel_Rana12-Jan-10 16:49
Sohel_Rana12-Jan-10 16:49 
GeneralMulti User serach Pin
Suresh Kumar.A20-Dec-09 22:27
Suresh Kumar.A20-Dec-09 22:27 
GeneralRe: Multi User serach Pin
Sohel_Rana21-Dec-09 0:14
Sohel_Rana21-Dec-09 0:14 
GeneralThe browse window Pin
explic_it15-Dec-09 2:05
explic_it15-Dec-09 2:05 
GeneralThe browse search Pin
explic_it7-Dec-09 19:39
explic_it7-Dec-09 19:39 
GeneralRe: The browse search Pin
explic_it7-Dec-09 21:08
explic_it7-Dec-09 21:08 
GeneralCheck this Site to add People Picker Programmatically [modified] Pin
Anoosh Rahman29-Dec-08 4:00
Anoosh Rahman29-Dec-08 4:00 
GeneralExcellent - Integrated With Web Part Nicely Pin
namaishi4-Nov-08 12:35
namaishi4-Nov-08 12:35 
GeneralGreat find.. Pin
wgyger28-Jun-08 8:54
wgyger28-Jun-08 8:54 
GeneralWhy screen shots Pin
#realJSOP4-Jun-08 0:22
mve#realJSOP4-Jun-08 0:22 
GeneralRe: Why screen shots Pin
Sohel_Rana4-Jun-08 1:59
Sohel_Rana4-Jun-08 1:59 
GeneralRe: Why screen shots Pin
Marc Clifton4-Jun-08 2:27
mvaMarc Clifton4-Jun-08 2:27 
GeneralRe: Why screen shots Pin
Sohel_Rana4-Jun-08 17:40
Sohel_Rana4-Jun-08 17:40 
GeneralRe: Why screen shots Pin
#realJSOP4-Jun-08 23:30
mve#realJSOP4-Jun-08 23:30 

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

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