Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search user by profile property

How i can search by profile property? MSDN say use ProfileSearchManager, but it not working. I want search users by MobilePhone property.

SPServiceContext serviceContext = SPServiceContext.GetContext(site);
UserProfileManager upm = new UserProfileManager(serviceContext);
ProfileSearchManager sp = ProfileSearchManager.GetProfileSearchManager(serviceContext);

string[] searchPattern = { "123" };
ProfileBase[] searchResults = sp.Search(searchPattern, ProfileSearchFlags.User);

foreach (ProfileBase profile in searchResults)
{
    Console.WriteLine(profile.DisplayName);
}
like image 943
Actimele Avatar asked Oct 31 '22 14:10

Actimele


1 Answers

 using (SPSite site = new SPSite(siteUrl))
            {
                    using (var qRequest = new KeywordQuery(site)
                    {
                        QueryText = "MobilePhone:*" +"123" , 
                        EnableQueryRules = true,
                        EnableSorting = false, 
                        SourceId = new Guid("Enter here Result Source Guid"),
                        TrimDuplicates = false
                    })
                    { 
                        //Get properties you want here
                        qRequest.SelectProperties.Add("FirstName");
                        qRequest.SelectProperties.Add("LastName");


                        SearchExecutor e = new SearchExecutor();
                        ResultTableCollection rt = e.ExecuteQuery(qRequest);
                        var tab = rt.Filter("TableType", KnownTableTypes.RelevantResults);
                        var result = tab.FirstOrDefault();

                        DataTable resultTable = result.Table;
                    }
}
like image 185
Habib Adıbelli Avatar answered Nov 09 '22 17:11

Habib Adıbelli