Wednesday, January 22, 2014

People Search using Keyword Query in SharePoint 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.

Friday, January 17, 2014

SHAREPOINT 2013 KEYWORD QUERY (KQL) CONTENT CLASS PROPERTY RESTRICTIONS

Using property restrictions in your KQL queries, you can restrict your search to only pull back certain things like calendar events for instance. This is a very powerful way to limit search results and get exactly what you are looking for.

You can use these in your query like this:
“lunch contentclass:STS_ListItem_Events“
This will return only calendar events with the word “lunch” in them.  Pretty powerful and pretty simple.

Here is a list of available content class items:

STS_Site –  Site Collection
STS_Web  –  Site (Web)
STS_List_850  –  Page Library
STS_ListItem_850  –  Page
STS_List_DocumentLibrary  –  Document Library
STS_ListItem_DocumentLibrary  –  Document Library Items
STS_List  –  Custom List
STS_ListItem  –  Custom List Item
STS_List_Links  –  Links List
STS_ListItem_Links  –  Links List Item
STS_List_Tasks  –  Tasks List
STS_ListItem_Tasks  –  Tasks List Item
STS_List_Events  –  Events List
STS_ListItem_Events  –  Events List Item
STS_List_Announcements  –  Announcements List
STS_ListItem_Announcements  –  Announcements List Item
STS_List_Contacts  –  Contacts List
STS_ListItem_Contacts  –  Contacts List Item
STS_List_DiscussionBoard  –  Discussion List
STS_ListItem_DiscussionBoard  –  Discussion List Item
STS_List_IssueTracking  –  Issue Tracking List
STS_ListItem_IssueTracking  –  Issue Tracking List Item
STS_List_GanttTasks  –  Project Tasks List
STS_ListItem_GanttTasks  –  Project Tasks List Item
STS_List_Survey  –  Survey List
STS_ListItem_Survey  –  Survey List Item
STS_List_PictureLibrary  –  Picture Library
STS_ListItem_PictureLibrary  –  Picture Library Item
STS_List_WebPageLibrary  –  Web Page Library
STS_ListItem_WebPageLibrary  –  Web Page Library Item
STS_List_XMLForm  –  Form Library
STS_ListItem_XMLForm  –  Form Library Item
urn:content-class:SPSSearchQuery  –  Search Query
urn:content-class:SPSListing:News  –  News Listing
urn:content-class:SPSPeople  –  People
urn:content-classes:SPSCategory  –  Category
urn:content-classes:SPSListing  –  Listing
urn:content-classes:SPSPersonListing  –  Person Listing
urn:content-classes:SPSTextListing  –  Text Listing
urn:content-classes:SPSSiteListing  –  Site Listing
urn:content-classes:SPSSiteRegistry  –  Site Registry Listing