Lets Learn

Opinion Matters

Posts Tagged ‘Search’

People Search using Keyword Query in SharePoint 2013

Posted by Ankush on April 24, 2013

Its been a long time since I have blogged. I was busy with some personal activities and now I have free time to start blogging!!

Here is the scenario.  I am developing an application which connects to user profile service a lot and I need to do a lots of filtering/searching based on the custom properties.

One solution could be to use user profile service and then do the work. You will find a lot of sample available online to do it.

One approach (which is efficient) is to use the result sources which has been created for us in SharePoint. Here is the code which I have written to get all the user profiles:

=============
private void button1_Click(object sender, EventArgs e)
        {
            SPSite sp = new SPSite("http://Test:8077/sites/esc/");
// For all employees Use DataTable dt = GetPeople(sp, "*”);         

   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 = 300;

            keywordQuery.SelectProperties.Add("AccountName");
            keywordQuery.SelectProperties.Add("BloodGroup");
            keywordQuery.SelectProperties.Add("Designation");
            keywordQuery.SelectProperties.Add("SubjectOfExpertise");
            keywordQuery.SelectProperties.Add("PhoneNumber");
            keywordQuery.SelectProperties.Add("MarriageAnniversary");
            keywordQuery.SelectProperties.Add("BoardLineNumber");
            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;

        }
========================

The B09A7990-05EA-4AF9-81EF-EDFAB16C4E31 is for People Search. Complete list can be found at:   http://sharepoint.ulitzer.com/node/2501170

If you want to search other sources, just change the guid.

Use the above code and let me know if this is helpful.

Posted in Search, SharePoint 2013 | Tagged: , , , , , | Leave a Comment »

 
%d bloggers like this: