Lets Learn

Opinion Matters

  • Subscribe

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 7 other followers

  • Blog Stats

    • 72,532 hits
  • Ankush

    Error: Twitter did not respond. Please wait a few minutes and refresh this page.

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: