承接MOSS各种工作流开发 联系人:王先生.电话:18618405729QQ:252385878 QQ群:41696750 MSN:wanghao-3@hotmail.com

导航

Wss搜索

  
                昨天在整理硬盘的时候,发现一个不错的Wss搜索Demo.都不记得什么时候出来的.然后自己在自己的机器上面测试下,感觉还不错哦。 .

根据用户权限搜索,不同的用户,看到的网站不一样哦。有权限区别的哦.是对列表上面的搜索.把代码帖出来希望下面有些对方可以帮助SharePoint的爱好者!

效果图:



呵呵,感觉是不是很不错?






至少我感觉很棒!!!

最后帖代码:这是是我的习惯,开发人员有代码还是容易理解些.我不习惯来虚东西!

CS代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;
using Microsoft.SharePoint;


namespace WSSsearch
{
    public partial class WSSSearch : System.Web.UI.UserControl
    {

        protected SPWeb _web;
        private string row = "<TR><TD><IMG SRC='{0}'</TD><TD><A HREF='{1}'>{2}</A></TD>" +
                             "<TD>{3}</TD><TD>{4}</TD></TR>";


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                this.FillListSites();

        }

        #region IUserControl Members

        public Microsoft.SharePoint.SPWeb SPWeb
        {
            get
            {
                return this._web;
            }
            set
            {
                this._web = SPContext.Current.Web;
                this._web = value;
            }
        }

        #endregion


        #region Private Members

        private void FillListSites()
        {
            //-- add all sites
            this.listSites.Items.Add(new ListItem("All Sites", string.Empty));
            //-- add current site
            this._web = SPContext.Current.Web;
            this.listSites.Items.Add(new ListItem("Current: " + this._web.Title, this._web.ID.ToString()));
            //-- add the subsites that can be accessed by the user
            foreach (SPWeb subweb in this._web.GetSubwebsForCurrentUser())
            {
                this.listSites.Items.Add(new ListItem(subweb.Title, subweb.ID.ToString()));
            }
        }

        private void Search()
        {
            //-- preparation
            string keyword = this.textBoxKeyword.Text;
            string scope = this.listSites.SelectedItem.Value;
            StringBuilder sb = new StringBuilder();
            //-- loop over
            if (keyword != string.Empty)
            {
                if (scope != string.Empty)
                {
                    //-- only go for the selected site
                  //  SPContext.Current.Web
                    SPWeb web = SPContext.Current.Web.Site.OpenWeb();
                    if (web != null)
                        this.SearchList(web, ref sb, keyword);
                }
                else
                {
                    //-- have the current site
                    this.SearchList(this._web, ref sb, keyword);
                    //-- and all the sub sites
                    foreach (SPWeb web in this._web.GetSubwebsForCurrentUser())
                    {
                        this.SearchList(web, ref sb, keyword);
                    }
                }
            }
            //-- output the result
            if (sb.ToString() == string.Empty)
                literalResults.Text = "<BR><B>Sorry, there are no results.</B>";
            else
                literalResults.Text = "<TABLE WIDTH='100%' STYLE='border:solid 1px silver'>" + sb.ToString() + "</TABLE>";
        }

        public void SearchList(SPWeb webToProcess, ref StringBuilder sb, string keyword)
        {
            SPWeb web =SPContext.Current.Web.Site.OpenWeb(webToProcess.ID);
            SPSearchResultCollection resLists = web.SearchListItems(keyword);
            foreach (SPSearchResult resList in resLists)
            {
                sb.AppendFormat(row, resList.IconUrl, resList.Url,
                    resList.Title, resList.ModifiedBy,
                    resList.DateLastModified.ToShortDateString());
            }
        }

        #endregion

        protected void buttonSearch_Click(object sender, EventArgs e)
        {
            this.Search();
        }


 



ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WSSSearch.ascx.cs" Inherits="WSSsearch.WSSSearch" %>
<P>
 <TABLE id="Table1" cellSpacing="1" cellPadding="2" width="100%" border="0">
  <TR>
   <TD>
    <P>Search for
     <asp:TextBox id="textBoxKeyword" runat="server"></asp:TextBox>&nbsp;in
     <asp:DropDownList id="listSites" runat="server" Width="176px" OnSelectedIndexChanged="listSites_SelectedIndexChanged"></asp:DropDownList>&nbsp;
     <asp:Button id="buttonSearch" runat="server" Text="Search" OnClick="buttonSearch_Click"></asp:Button></P>
   </TD>
  </TR>
  <TR>
   <TD>
    <asp:Literal id="literalResults" runat="server"></asp:Literal></TD>
  </TR>
 </TABLE>
</P>
<P>&nbsp;</P>



代码不是最后.只是给我们开发同胞们.找不到demo的时候,有一个参考的!


                  

posted on 2007-11-29 12:26  A A  阅读(748)  评论(1编辑  收藏  举报