其中GetTopKeywords()方法就是获取在搜索中心最热门搜索查询。前提要引用两个dll文件,Microsoft.Office.Server.WebAnalytics.dll和Microsoft.Office.Server.WebAnalytics.UI.dll
protected DataTable GetTopKeywords(SPSite site) { AggregationContext aggregationContext = AggregationContext.GetContext(site); List<ViewParameterValue> viewParamsList = new List<ViewParameterValue>(); viewParamsList.Add(new ViewParameterValue("StartDateId", DateTimeToDateId(DateTime.UtcNow.AddMonths(-1)))); viewParamsList.Add(new ViewParameterValue("EndDateId", DateTimeToDateId(DateTime.UtcNow))); viewParamsList.Add(new ViewParameterValue("ScopeName", null)); DataPacket dataPacket = FrontEndDataRetriever.QueryData(aggregationContext, null, "fn_WA_GetTopSearchQueries", viewParamsList, null, GetSortOrder("Frequency", OrderType.Descending), 1, 2000, false); return dataPacket.DataTable; } public static int DateTimeToDateId(DateTime dt) { if (string.IsNullOrEmpty(dt.ToString())) { return 0; } return int.Parse(dt.ToString("yyyyMMdd", CultureInfo.InvariantCulture), CultureInfo.InvariantCulture); } private static List<SortOrder> GetSortOrder(string sortColumn, OrderType order) { List<SortOrder> list = new List<SortOrder>(); SortOrder item = null; item = new SortOrder(sortColumn, order); list.Add(item); return list; }
效果如下图所示(红框中是重点):