技术开发 频道

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


【IT168技术文档】

  在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); }
0
相关文章