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.
The SharePoint provided user/group picker contains four controls as shown in figure 1 and described below:
- A textbox to enter user/group name.
- A check-name icon to check if the user/group name is valid.
- A browse icon to allow user/group to be searched.
- A hidden control which shows messages when user/group name not found.
- 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
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
_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
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,
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="False" Error="" Separator=";" MaxHeight="3">
<Entity Key="ONIRBAN\sohel" DisplayText="ONIRBAN\sohel" IsResolved="True
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="UserGroupBrowse, Version=22.214.171.124, Culture=neutral,
TypeName="*" Safe="True" />
Then, add the following entry in the
<assemblies> section of the web.config:
<add assembly="UserGroupBrowse, Version=126.96.36.199, Culture=neutral,
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.
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:UpdatePanel ID="ActionItemPanel" runat=""server"">
Then, your control markup will be as shown below:
I have tried to share my knowledge with all other SharePoint developers. Thanks.