WebDrawer search form lookup items

As you can see in the image below the lookup items displayed for an Additional Field in a search form are not sorted by name.  This is not a problem when there are only four items but is when there are many.  The good news is that, with a little bit of code, we can fix this.

The process

Find the correct WebDrawer view

In the WebDrawer folder find the file Views\FormSearch.cshtml (or Views\Search.cshtml in 8.3 and later).

Add code to fetch items

Near the top of the page find this line of code:

HttpContext.Current.Response.AppendCookie(new HttpCookie("HPRM_webDrawer_SearchForm", sfName));

On the next line add this code:

    var roadSurfaces = TrimHelper.Search<LookupItem>(
            BaseObjectTypes.LookupItem,
            "lkiSet:\"Road Surfaces\"",
            pageSize:1000,,
            sortBy:new string[] { "lkiName" },
            properties: new string[] { "Name" }).Select(li => li.Name);

Now edit this code so that instead of searching for all the items in the Lookup Set 'Road Surfaces' it uses the name of your lookup set.  If you want to also change the name of the variable from 'roadSurfaces' to something else, if you do this then code you add later will also need to be modified.

 

Create a SELECT element

Find the code that looks like this:

Prepend this code:

if (pageItem.Id == "9000000000")
{
   <select name="@pageItem.ClauseName" multiple class="wd-search-field">
       @foreach (string val in roadSurfaces)
       {
           <option>@val</option>
        }
    </select>
 } else

So that now it looks like this:

Set Field Uri

Modify the code pasted above so that instead of '9000000000' you have the Uri of the additional field you wish to use the lookup items for.

The Result

The result of adding these two code blocks is that you have complete control of the Lookup Items are displayed for a particular field.  You can add the blocks multiple times for multiple fields.

Warning

Keep a record of any changes you make to your WebDrawer templates so that you can re-apply them when you upgrade.