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, "*”);
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")
};
{
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");
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;
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.