2007/09/01 19:10

MOSS Custom 검색을 위한 Webpart를 만들기 위한 예제 코드

MOSS Custom 검색을 위한 Webpart를 만들기 위한 예제 코드


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Xml;
using System.Xml.Serialization;
using System.Data;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.Office.Server;
using Microsoft.Office.Server.Search.Query;
using System.Runtime.InteropServices;


namespace CustomSearchWebPart
{

    [ToolboxData("<{0}:clsSearchQuery runat=server></{0}:clsSearchQuery>")]
    [XmlRoot(Namespace = "CustomSearchWebPart")]
    [Guid("486838d1-843e-44d8-b56f-40e743504faa")]
    public class clsSearchQuery : WebPart
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]

        Button cmdSearch;
        TextBox txtQueryText;
        Label lblQueryResult;
        DataGrid grdResults;

        protected override void CreateChildControls()
        {
            Controls.Clear();
            txtQueryText = new TextBox();
            this.Controls.Add(txtQueryText);
            cmdSearch = new Button();
            cmdSearch.Text = "Start Search";
            cmdSearch.Click += new EventHandler(cmdSearch_Click);
            this.Controls.Add(cmdSearch);
            lblQueryResult = new Label();
            this.Controls.Add(lblQueryResult);

        }

        void cmdSearch_Click(object sender, EventArgs e)
        {
            if (txtQueryText.Text != string.Empty)
            {
                keywordQueryExecute(txtQueryText.Text);

            }
            else
            {
                lblQueryResult.Text = "You must enter a search word.";
            }

        }


        //Execute Keyword Query
        private void keywordQueryExecute(string strQueryText)
        {
            KeywordQuery kRequest = new KeywordQuery(ServerContext.Current);
            string strQuery = "author:" + strQueryText;
            kRequest.QueryText = strQuery;
            //to return relevant results
            kRequest.ResultTypes |= ResultType.RelevantResults;
            ResultTableCollection resultTbls = kRequest.Execute();
            if ((int)ResultType.RelevantResults != 0)
            {
                ResultTable tblResult = resultTbls[ResultType.RelevantResults];
                if (tblResult.TotalRows == 0)
                {
                    lblQueryResult.Text = "No Search Results Returned.";
                }
                else
                {
                    ReadResultTable(tblResult);
                }

            }
        }

        void ReadResultTable(ResultTable rt)
        {
            DataTable relResultsTbl = new DataTable();
            relResultsTbl.TableName = "Relevant Results";
            DataSet ds = new DataSet("resultsset");
            ds.Tables.Add(relResultsTbl);
            ds.Load(rt, LoadOption.OverwriteChanges, relResultsTbl);
            fillResultsGrid(ds);
        }

        private void fillResultsGrid(DataSet grdDs)
        {
            //Instantiate the DataGrid, and set the DataSource
            grdResults = new DataGrid();
            grdResults.DataSource = grdDs;

            //Set the display properties for the DataGrid
            grdResults.GridLines = GridLines.None;
            grdResults.CellPadding = 4;
            grdResults.Width = Unit.Percentage(100);
            grdResults.ItemStyle.ForeColor = Color.Black;
            grdResults.ItemStyle.BackColor = Color.AliceBlue;
            grdResults.ItemStyle.Font.Size = FontUnit.Smaller;
            grdResults.ItemStyle.Font.Name = "Tahoma";
            grdResults.HeaderStyle.BackColor = Color.Navy;
            grdResults.HeaderStyle.ForeColor = Color.White;
            grdResults.HeaderStyle.Font.Bold = true;
            grdResults.HeaderStyle.Font.Name = "Tahoma";
            grdResults.HeaderStyle.Font.Size = FontUnit.Medium;

            /*Turn off AutoGenerate for the columns, so the DataGrid
            doesn't automatically bind to all of the columns
            in the search results set.
            Then create and configure only the columns you want to
             include in the DataGrid.
            */
            grdResults.AutoGenerateColumns = false;
            HyperLinkColumn colTitle = new HyperLinkColumn();
            colTitle.DataTextField = "Title";
            colTitle.HeaderText = "Title";
            colTitle.DataNavigateUrlField = "Path";
            grdResults.Columns.Add(colTitle);
            BoundColumn colAuthor = new BoundColumn();
            colAuthor.DataField = "Author";
            colAuthor.HeaderText = "Author";
            grdResults.Columns.Add(colAuthor);

            //Bind the data to the DataGrid
            grdResults.DataBind();
            //Add the DataGrid to the controls
            Controls.Add(grdResults);
        }
    }
}

크리에이티브 커먼즈 라이선스
Creative Commons License

'Microsoft > SPS/MOSS' 카테고리의 다른 글

Active Directory ㅠ_ㅠ  (0) 2007/09/06
MOSS 테크넷  (0) 2007/09/05
Active directory 제어  (0) 2007/09/05
MOSS Custom 검색을 위한 Webpart를 만들기 위한 예제 코드  (0) 2007/09/01
MOSS SharePoint Server 2007 SDK  (0) 2007/08/30
MOSS SmartPart  (0) 2007/08/28
올블로그추천버튼 블코추천버튼 블로그뉴스추천버튼 믹시추천버튼 한RSS추가버튼 구글리더기추천버튼


이 포스팅이 도움이 되었다면 구글에서 관련 정보를 찾아 보세요 ^^


Trackback 0 Comment 0

Trackback : http://i-ruru.com/trackback/149 관련글 쓰기