Introduction
In this article, I will provide information on how to get user suggestions in a people picker control by querying the LDAP using Novell Directory Nuget Package in a Razor pages. In order to see how to create a .Net Core Web Application with Razor Pages and retrieve data from SQL Server using Entity Framework, you can visit my previous article.
Below are the software/concepts used in this document.
- Visual Studio 2019
- Razor Pages
- .Net Core 2.1
- Net Core Web Application
- C# Language
- Novell Directory Nuget Package
Many times we get a requirement where the user wants to search the LDAP in a text control and get suggestions from LDAP. This requirement can be handled in .Net Core by creating a people picker-like control and querying the LDAP using the Novell Directory Nuget Package. Below is a step-by-step description of how to achieve this in .Net Core Razor Pages.
Open your project in Visual Studio 2019
In my case, I am opening the earlier-created project where Razor pages are present.

People Picker control code in cshtml page of .Net Core Web Application
In my example, I will be storing the value of people picker in “strGateKeeper1”
- <tr>
- <td colspan="1" class="form-label">
- <div class="rTableCell-label">
- <label asp-for="structure.strGateKeeper1"></label>
- </div>
- </td>
- <td colspan="1" class="control">
- <div class="rTableCell-control">
- <input asp-for="structure.strGateKeeper1" />
- <input asp-for="structure.Email_GateKeeper1" type="hidden" />
- </div>
- </td>
- </tr>
Add the below line in the script section of the same cshtml code where you inserted above code. The below code would call the Javascript method for the people picker autocomplete and call the controller method as well. In my example: “structure_strGateKeeper1” is the control name and “/api/Lists/LoadPeoplePicker/” is the method I want to call from my “Lists” controller.
- <script>
- $(document).ready(function () {
- //People Picker (New form) -- Set the attribute of the ppl field & attach the autocomplete action
- PeoplePickerFor("structure_strGateKeeper1", "/api/Lists/LoadPeoplePicker/");
- });
- </script>
“PeoplePickerFor” method Code in the javascript file
Insert below code in a javascript file to have the PeoplePickerFor method which we called from the previous cshtml file. This method would call the controller method along with the search term.
- // -----------------------------------------------------------------
- // Client method to enable autocomplete for the people picker text field and calling the controller method
- // -----------------------------------------------------------------
- function PeoplePickerFor(TxtBox, SourceUrl) {
- $("#" + TxtBox).keyup(function (e) {
- if (e.keyCode == 8 || e.keyCode == 46) {
- var EmailAttrVal = $("#" + TxtBox).attr(TxtBox + "_email");
- if (EmailAttrVal) {
- $("#" + TxtBox).attr(TxtBox + "_email", "");
- $("#" + TxtBox).attr(TxtBox + "pp_resolved", false);
- }
- }
- });
- $("#" + TxtBox).autocomplete({
- source: function (request, response) {
- $.ajax({
- url: SourceUrl + request.term,
- dataType: "json",
- success: function (data) {
- console.log(data);
- var ErrorVal = $("#" + TxtBox + "pp_errordivid").text();
- if (data.length < 1) {
- if (ErrorVal == "" || ErrorVal == "undefined") {
- $("#" + TxtBox).after("<div style='color:red;font-weight:bold' id='" + TxtBox + "pp_errordivid'>No Valid Users Found</div>");
- }
- $("#" + TxtBox).attr(TxtBox + "pp_resolved", false);
- }
- else {
- $("#" + TxtBox + "pp_errordivid").remove();
- }
- response($.map(data, function (v, i) {
- return {
- label: v.displayName,
- value: v.displayName,
- text: v.email
- };
- }));
- }
- });
- },
- minLength: 2,
- select: function (event, ui) {
- if (!$.trim(ui.item.text)) {
- $("#" + TxtBox).attr(TxtBox + "_email", "");
- $("#" + TxtBox).attr(TxtBox + "pp_resolved", false);
- $("#" + TxtBox).after("<div style='color:red;font-weight:bold' id='" + TxtBox + "pp_errordivid'>Not a valid user</div>");
- }
- else {
- $("#" + TxtBox).attr(TxtBox + "_email", ui.item.text);
- $("#" + TxtBox).attr(TxtBox + "pp_resolved", true);
- }
- }
- });
- }

Join the conversation! Your thoughts help the community grow.