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

Former Member
Former Member

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

Parents Reply
  • Former Member
    0 Former Member in reply to Former Member

    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

Children
  • Former Member
    0 Former Member in reply to Former Member

    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.

  • Former Member
    0 Former Member in reply to Former Member

    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?

  • Former Member
    0 Former Member in reply to Former Member

    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?

  • Former Member
    0 Former Member in reply to Former Member

    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?

  • Former Member
    0 Former Member in reply to Former Member

    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.

  • Former Member
    0 Former Member in reply to Former Member

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

  • Former Member
    0 Former Member in reply to Former Member

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

  • Former Member
    0 Former Member in reply to Former Member
    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.

    Hi Stephen,

    Could you please explain this procedure a bit more. What would I pass into my widget extension method, and what would I pass out? What would be the pseudocode for the procedure?

  • You need a custom table to store whatever you want to sort y and the contentId and any associated code to make your call.  That list of Ids can be passed to the User list  and be returned in the same order.

    https://community.telligent.com/community/11/w/api-documentation/63544/userslistoptions-in-process-api-supplementary-type

  • Former Member
    0 Former Member in reply to Patrick M.

    I am returning the sorted Users list from my widget extension, but the users are no longer being filtered on the Member Search page. How can I get the filtering working again?