Sort the search results on the member search by a custom field

Can the search results on the member search page be sorted by an ExtendedAttributes or ProfileFields field?

Parents Reply Children
  • There is limited logic to perform on-page sorting in Velocity,

    What is this logic?

    You could also expose your own widget extension that could collect all the users from the User In Process API and re-sort them by your desired field.

    How would I go about this?

  • 1.  Velocity extensions return C# objects that can be manipulated with C# methods, but the availablity of certain methods at render time may not be guaranteed. And again - the sorting would be limited based on a paged set of results based on another sort.

    2. I provided some links that should be able to get you started - you'll need to create a project/solution in Visual Studio and build custom code to drop into your site's Web/bin folder. The links go to the Developer Training area and the full API Reference respectively, which should be most of the general information you need. Do you have specific questions here?

  • I know how to write a widget extension, will I be able to access the extendedattributes of a user object in C#?  What would I pass into the widget extension? Is it the search results list returned from this statement? 

    #set ($searchResults = $core_v2_searchResult.List($searchListOptions))

    #foreach ($result in $searchResults)
    	#set ($user = false)
    	#foreach ($resultUser in $result.Users)
    		#set ($user = $resultUser)
    	#end

  • Using the in-process API in the widget extension, you can access all properties of the user. So your pseudocode would be something like:

    1. Retrieve all users page by page (again: costly based on your number of users)
    2. Sort them by comparing on your desired field
    3. Return them to Velocity via your widget extension, either as a full list or paged.

    Again to reiterate, retrieving all users in this way, especially if you then page results of your new sorted list, is going to be quite expensive. At that point it might be better to set up a custom data store (i.e., your own database table) that keeps your custom fields referenced by userid, and then use the user list API and provide a list of ids via the ContentIds option with SortBy=ContentIdsOrder. Then you can return that pre-paged, pre-sorted list via your widget extension.

  • This is my first attempt at a widget extension using the in-process API, but it doesn't seem to return any results after the sorting:

    The widget extension C# code:

    using Telligent.Evolution.Extensibility.UI.Version1;
    using Telligent.Evolution.Extensibility.Api.Entities.Version1;
    using System;
    using System.Text;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace Utils
    {
        public class Utilities
        {
            public string Base64Encode(string base64Decoded)
            {
                byte[] data = ASCIIEncoding.ASCII.GetBytes(base64Decoded);
                string base64Encoded = Convert.ToBase64String(data);
                return base64Encoded;
            }
    
            public string ConvertTimestamp(int ts)
            {
                DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(ts).ToLocalTime();
                return dt.ToString("MMM dd, yyyy");
            }
    
            public List<SearchResult> SortSearchResults(List<SearchResult> ls)
            {
                List<SearchResult> SortedList = ls.OrderBy(o => o.Users[0].ExtendedAttributes["SortOrder"]).ToList();
                return SortedList;
            }
    
    
        }
    
        public class UtilsWidgetExtension : IScriptedContentFragmentExtension
        {
            #region IScriptedContentFragmentExtension Members
    
            public string ExtensionName
            {
                get { return "Ibby_v1_Utils"; }
            }
    
            public object Extension
            {
                get { return new Utilities(); }
            }
    
            #endregion
    
            #region IPlugin Members
    
            public string Name
            {
                get { return "Utilities"; }
            }
    
            public string Description
            {
                get { return "Utilities"; }
            }
    
            public void Initialize()
            {
            }
    
            #endregion
        }
    }
    

    The Velocity code:

    #set ($searchResultsOriginal = $core_v2_searchResult.List($searchListOptions))
    #set($searchResults = $Ibby_v1_Utils.SortSearchResults($searchResultsOriginal))
    
    ## Render Search Results
    
    #set ($hasMore = false)
    #set ($currentPagedQuantity = ($searchResults.PageIndex + 1) * $searchResults.PageSize)
    #if ($searchResults.TotalCount > $currentPagedQuantity)
    	#set ($hasMore = true)
    #end

    What could the problem be?

  • Have you tried debugging and stepping through to confirm that the method is called and the fields you are using to sort compare are there?

  • I'm not sure I know how to reproduce the user data in my Telligent installation on the C# side for use in debugging and stepping through. Any thoughts?

  • when you copy your .dll into Web/bin, also include the .pdb of the same name. Then you can attach to w3wp process in Visual Studio and set a breakpoint in the sort method.

  • The method is not getting called. I can't figure out why.

  • It appears to be working now. I am now passing the search results list as an array instead of a list. Thanks.