获取最热门搜索查询
简介:
Sharepoint中有强大的搜索功能,支持全文搜索。我们在这就在基于强大的搜索功能上开发个搜索热词webPart,配合搜索按钮使用使用户使用更方便。
实现方法:
首先创建webpart
在webpart前端添加显示最热此的控件
<span style="font-size:18px;"><scripttype="text/javascript">
function searchHotKeys(k) {
var key =encodeURIComponent(k.innerText || k.textContent);
window.open("/searchcenter/results.aspx?k=" + key);
}
</script></span>
<span style="font-size:18px;"><divclass="foot_label">
<spanid="search_hotkey"runat="server"></span>
</div></span>
后台代码:
控件load事件中的方法
<span style="font-size:18px;"> protectedvoid Page_Load(object sender, EventArgs e)
{
string str =GetPopularKeys();
if (!string.IsNullOrEmpty(str))
{
search_hotkey.InnerHtml = "热门搜索词:" + str;
}
}</span>
自定义的函数
///<summary>获取最热关键词
///
///</summary>
///<returns></returns>
publicstaticstring GetPopularKeys()
{
StringBuilder sb = newStringBuilder();
// DateTime time =Convert.ToDateTime("2014-07-05 00:00:00");
SPSecurity.RunWithElevatedPrivileges(delegate
{
var context = SPServiceContext.GetContext(SPContext.Current.Site);
var searchProxy =context.GetDefaultProxy(typeof(SearchServiceApplicationProxy)) asSearchServiceApplicationProxy;
var topQueries =searchProxy.GetSearchReport(1, Guid.Empty, Guid.Empty, DateTime.Now, false, 40);
foreach (var query in topQueries)
{
var popkey =query.Query;
if (popkey.Length<= 10 && !string.IsNullOrEmpty(popkey)&& sb.Length < 300)
{
sb.Append("<aοnclick='searchHotKeys(this)' style='cursor:pointer;' >" + popkey + "</a>" + " ");
}
}
});
return sb.ToString();
}
其次创建feature
创建feature,将自己开发的webpart包括在feature中。通过激活feature添加到页面就可以使用了。
总结
对用到的对象以及方法进行介绍。
SPSecurity对象中的
方法RunWithElevatedPrivileges
功能:最高达权限执行方法中的函数,不管当前用户是否包含该权限。
SPServiceContext对象
方法:GetDefaultProxy
功能:Returns the default service application proxy for the specifiedtype.
得到对象中的方法:getsearchreport方法
功能:Gets data for a search report, including topqueries that apply to that particular search report.