sharepoint获得热门搜索关键字

获取最热门搜索查询

简介:

     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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值