C# – Displaying Custom User Properties with KeywordQuery that uses Managed Metadata in SharePoint 2010

csharepointsharepoint-2010

Background:

I am using KeywordQuery to perform a people search in a custom webpart in SharePoint 2010. I am searching for specific user profile properties. These properties are mapped to Managed Properties in the Search Service Application. I want to output these properties in the search result. This works fine for standard properties and custom user properties that doesn't use managed metadata.

Problem:

However, the problem is: User Profile Properties that uses Managed Metadata term sets is not displayed in the search results. I am not sure if the problem has to do with MM, but it seems likely, since other custom properties shows up. The kicker is that the search itself seems to work fine. I get the excpected people when I search for user profile properties that uses MM.

Relevant code:

In the code below, I use SelectProperties to add columns for the properties I want to show. The custom properties Regular_Property and MM_Property are both Managed Properties. However, of these two, only Regular_Property is showing up in the GridView. Searching on the MM_Property, for example keywordQueryText = MM_Property:"Some Value", seems to work fine.

private void ExecuteKeywordQuery(string keywordQueryText)
        {
            SearchServiceApplicationProxy SSAProxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.
               GetProxy(SPServiceContext.GetContext(SPContext.Current.Site));

            KeywordQuery keywordQuery = new KeywordQuery(SSAProxy);
            keywordQuery.ResultsProvider = SearchProvider.Default;
            keywordQuery.QueryText = keywordQueryText;
            keywordQuery.ResultTypes |= ResultType.RelevantResults; 
            //keywordQuery.ResultTypes = ResultType.RelevantResults;
            keywordQuery.HiddenConstraints = "scope:\"People\"";
            //keywordQuery.KeywordInclusion = KeywordInclusion.AnyKeyword;

            keywordQuery.SelectProperties.Add("Title");
            keywordQuery.SelectProperties.Add("Path");
            keywordQuery.SelectProperties.Add("MobilePhone");
            keywordQuery.SelectProperties.Add("Regular_Property");
            keywordQuery.SelectProperties.Add("MM_Property");


            ResultTableCollection searchResults = keywordQuery.Execute();

            if (searchResults.Exists(ResultType.RelevantResults))
            {
                ResultTable searchResult = searchResults[ResultType.RelevantResults];
                DataTable result = new DataTable();
                result.TableName = "SearchResults";
                result.Load(searchResult, LoadOption.OverwriteChanges); 


                PopulateResultsGrid(result);
            }
private void PopulateResultsGrid(DataTable resultsTable)
        {
            gridSearchResults = new GridView();
            gridSearchResults.DataSource = resultsTable;
            gridSearchResults.DataBind();
            Controls.Add(gridSearchResults);
        }

Does anybody know how to make this work? I have tried numerous attempts at resetting the index, full crawls, incremental crawls, user profile synchronizations etc. It still does not seem to work.

Best Answer

I figured it out eventually, and the answer was annoyngly simple. Since the MM_Property is multivalued, the datatype of values stored in the column (as returned by result.Columns["MM_Property"].DataType), is a string array, and not a string.

Related Topic