Wednesday, April 24, 2013

Working with People Search, KeywordQuery, SearchExecutor, ResultTableCollection in SharePoint 2013

using Microsoft.Office.Server.Search.Query;
using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            SPSite sp = new SPSite("http://server:portno/sites/esc/");
            DataTable dt = GetPeople(sp, "EmployeeID:" + "101");
            dataGridView1.DataSource = dt;
        }
        private DataTable GetPeople(SPSite spSite, string queryText)
        {
            var keywordQuery = new KeywordQuery(spSite)
            {
                QueryText = queryText,
                KeywordInclusion = KeywordInclusion.AllKeywords,
                SourceId = new Guid("B09A7990-05EA-4AF9-81EF-EDFAB16C4E31")
            };
            keywordQuery.RowLimit = 7;
            keywordQuery.SelectProperties.Add("AccountName");
            keywordQuery.SelectProperties.Add("EmployeeID");
            SearchExecutor e = new SearchExecutor();
            ResultTableCollection rt = e.ExecuteQuery(keywordQuery);
            var tab = rt.Filter("TableType", KnownTableTypes.RelevantResults);
            var result = tab.FirstOrDefault();
            DataTable DT = result.Table;
            return DT;
        }
    }
}

No comments:

Post a Comment