按个人资料属性搜索用户

时间:2014-11-07 05:14:51

标签: c# sharepoint sharepoint-2013

我如何通过个人资料搜索? MSDN说使用ProfileSearchManager,但它无法正常工作。 我希望通过MobilePhone属性搜索用户。

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);
}

1 个答案:

答案 0 :(得分:2)

 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;
                    }
}