Using SharePoint PeoplePicker control in custom ASP.NET pages

I was developing custom ASP.NET page for a SharePoint project, and the page uses SharePoint PeoplePicker control. I needed to manipulate the control on the client side based on the user inputs.

PeoplePicker Picker is a complex control and the difficult bit is that it contains many controls on the page (use the page source viewer to see the HTML tags generated).

So getting into the right bit is tricky and also the default JavaScript functions like, control.disabled; control.focus(); will not work with PeoplePicker control. After some trial and error I came up with the solution to manipulate the control using JavaScript.  Here I am posting the JavaScript code snippet to enable/disable the PeoplePicker Control:

function ToggleDisabledPeoplePicker(element, isDisabled)
{
    try
    {
        element.disabled = isDisabled;
    }
      
    catch(exception)
    {}
      
    if ((element.childNodes) && (element.childNodes.length > 0))
    {
        for (var index = 0; index < element.childNodes.length; index++)
        {
            ToggleDisabledPeoplePicker(element.childNodes[index], isDisabled);
        }
    }
}

//to disable the control
ToggleDisabledPeoplePicker(document.getElementById("<%=txtMRA.ClientID%>"), true);

The script shown below can be used to set focus back to the PeoplePicker control from custom JavaScript validation function:

var found = false;
       
function SetFocusToPeoplePicker(element)
{
    try
    {
        if (element.id.lastIndexOf("upLevelDiv") !=-1)
        {
            element.focus();
            found = true;
            return;
        }
    }
       
    catch(exception)
    {}
       
    if ((element.childNodes) && (element.childNodes.length > 0))
    {
        for (var index = 0; index < element.childNodes.length; index++)
        {
            if (found)
            {
                found = false;
                break;
            }
        
            SetFocusToPeoplePicker(element.childNodes[index]);
        }
    }
}

  • Jignesh
This article is part of the GWB Archives. Original Author: Jignesh Gangajaliya

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.