如何在SharePoint里自定义WebPart将搜索结果显示在同一个界面

来源:互联网 发布:天正电气绘图软件 编辑:程序博客网 时间:2024/06/06 15:04

在SharePoint里面,在你搜索的时候,是可以订制搜索结果的显示页面的。但是,在选择This Site和This List这两个Scope时,他的搜索结果总是显示在SharePoint默认的页面里。怎么让他们都显示在同一个页面里呢?其实我们可以做一个我们自己的WebPart来做这件事。

首先画UI,一个DropDownList,ID是scopeDropDownList,用来存放当前用于搜索的scope。紧接着的是一个TextBox,ID是searchKeyWordTextBox,用来存放当前用于搜索的关键字。最后是一个ImageButton,ID是searchButton,用来触发搜索事件。

接下来就开始实现后台代码了。
首先我们要获取当前可用的scope.
ServerContext serverctx = ServerContext.GetContext(SPContext.Current.Site);

SearchContext searchctx = SearchContext.GetContext(serverctx);

Scopes _scopes = new Scopes(searchctx);

注意,这里得到的scope并非都是可以用的。首先很多都是隐藏的,你不因该把他们显示出来。其次,有些scope并非所有站点都可以使用,你还必需判定这个scope是否属于当前站点。

清空scopeDropDownList,开始添加scope信息.
scopeDropDownList.Items.Clear();

第一个添加的是This Site: site title,这个scope是我们人为做出来的,在你上面得到的_scopes对象中是没有的。他的搜索范围是当前Site.

string itemText = "This Site: " + SPContext.Current.Site.RootWeb.Title;
//-1 means this site, -2 means this list
scopeDropDownList.Items.Add(new ListItem(itemText, "-1"));

如果你搜索的时候所处的页面是在List中的话,你还需要添加第二项,This List: list title.这个scope也是我们人为做出来的。他的搜索范围是当前List.

if (SPContext.Current.List != null)//判断是否存在于一个List中
{
 itemText = "This List: " + SPContext.Current.List.Title;
        //-1 means this site, -2 means this list
        scopeDropDownList.Items.Add(new ListItem(itemText, "-2"));
}

接着添加其他的scope,包括All Sites, People之类的。

foreach (Scope scope in _scopes.AllScopes)
{
 if (scope.OwningSite != null && scope.OwningSite.Url !=SPContext.Current.Site.Url)
         continue;

 if (!scope.DisplayInAdminUI)
         continue;

 item = new ListItem(scope.Name, scope.ID.ToString());
 scopeDropDownList.Items.Add(item);
}

这样,你就做好了一个SearchBox的外观,并且可以显示scope信息,现在来实现Search功能。其实,我们在实现这个WebPart的时候,并不需要去实现搜索的过程。他的原理很简单,你只需有一个包含可以显示Search Result的Web Part的页面,把搜索的参数放在url里传过去,那个显示Search Result的Web Part会自己去搜索的。所以我们要做的只不过是拼正确的字符串而已。给ImageButton添加一个Click事件,叫searchButton_Click,在事件里添加以下代码。

if (scopeDropDownList.SelectedItem == null)
    return;
            
string _rootUrl = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port;
string url = _searchResultPageUrl + "?k=" + searchKeyWordTextBox.Text;
int id = int.Parse(scopeDropDownList.SelectedItem.Value);
                
if (id >= 0)
{
 url = url + "&s=" + scopeDropDownList.SelectedItem.Text;
}
else
{
 if (scopeDropDownList.SelectedItem.Text.Contains("This List"))
 {
         string u = _rootUrl;

          if (HttpContext.Current.Request.Url.Segments.Length >0 && HttpContext.Current.Request.Url.Segments[0] != "/")
                        u += "/" + HttpContext.Current.Request.Url.Segments[0];
         if (HttpContext.Current.Request.Url.Segments.Length > 1 && HttpContext.Current.Request.Url.Segments[0] == "/")
                        u += HttpContext.Current.Request.Url.Segments[0] + (HttpContext.Current.Request.Url.Segments[1].Split('/'))[0];

         url = url + "&cs=This List&u=" + u;
 }
 else if (scopeDropDownList.SelectedItem.Text.Contains("This Site"))
 {
         url = url + "&cs=This Site&u=" + _rootUrl;
 }
}

Response.Redirect(url);

最后记得把_searchResultPageUrl用一个属性暴露出去,把我们要显示结果的页面的Url赋给他。使得我们可以定制一个显示结果的页面,把所得的搜索结果,不管是This List, This Site还是All Sites, People,现在都会显示在这个页面里了。

至此,一个我们自定义的实现Search功能的WebPart就此完成。

 

本文转自:http://www.cnblogs.com/minsentinel/archive/2007/12/04/982366.html

原创粉丝点击