SharePoint UserProfileManager.Search Method Sample

I was recently working with a customer looking to create an enterprise library solution which enabled their developers to search the SharePoint enterprise User Profile Service Application.  This endeavor brought us to the UserProfileManager.Search method which is a part of the Microsoft.Office.Server.UserProfiles namespace.  The documentation on MSDN for this method is very scarce hence we had no way to know what to pass to this method as a search string.  Below, I describe how this method can be used.  I want to give special thanks to Jim Crowley and the members of the product team who helped round up this information.

This method takes a string and then looks for matches among the searchable properties of profile objects that inherit from ProfileBase. The searchable properties are 'FirstName', 'LastName', 'PreferredName', 'UserName', 'Office', 'Title', 'Department', 'WorkEmail', 'SPS-SipAddress', and 'AccountName'. The method performs a “begins with” sort of search, so you get a match when the search pattern strings match the first characters in any of the searchable properties. For example, the search pattern “Micr Corp” would return profiles that contain properties containing “Microsoft Corporation," and "james 16” will return any profile where the user has a name which begins with “James” located in Building 16. The search pattern can never be an empty string. Also, the above properties are currently the only searchable properties. Your own custom profile properties cannot be used.

The overloads can take a single string or an array of strings. An array of strings – like the space-delimited strings in the examples above -- makes the query more restrictive (so it performs an “and” instead of an “or” with the multiple search patterns). The method can also take an OrganizationProfile object (along with a search string or an array of search strings), in which case it will restrict the search to a specific organization.

Here is a very simple sample:

  SPSite siteColl = SPContext.Current.Site;

            SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);

            UserProfileManager upm = new UserProfileManager(serviceContext);

            //Use either the string or the array of strings

            //string[] searchPattern = { “User”, “UserOne”};

            string searchPattern = "User";

            ProfileBase[] searchResults = upm.Search(searchPattern);

            foreach (ProfileBase profile in searchResults)