Sorting

Adding custom sorting on federated locations in the CoreResultsWebPart can be a little bit tricky. Here is an example of how it could be done:

using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using System.Xml.XPath;
using Hvam.SharePoint.Search.Query;
using Microsoft.Office.Server.Search.WebControls;
using SortDirection = Microsoft.Office.Server.Search.Query.SortDirection;

namespace SharePointProject
{
    [ToolboxItemAttribute(false)]
    public class MyCoreResultsWebPart : CoreResultsWebPart
    {
        [Personalizable(PersonalizationScope.Shared), WebBrowsable(true)]
        public virtual string SortField { get; set; }

        protected override XPathNavigator GetXPathNavigator(string viewPath)
        {
            var dataSource = (CoreResultsDatasource) DataSource;

            var view = dataSource.GetView();

            foreach (var location in view.LocationList)
            {
                var sortList = location.GetSortList();
                sortList.Clear();
                sortList.Add(SortField, SortDirection.Ascending);
            }

            return base.GetXPathNavigator(viewPath);
        }
    }
}
Overwrite GetXPathNavigator() and use the extension method GetSortList() from Hvam.SharePoint.Search.Query. The same approach can be used if you are using the QueryManager.

Last edited Feb 13, 2012 at 9:13 PM by allanhvam, version 5

Comments

No comments yet.