Monday, 10 October 2011

User Profiles: Search in User Profiles

Searching for User Profiles that match a given value is, actually, very easy.

Here's a quick example:


public ProfileBase[] GetUserProfilesByWordByProfileManager(string searchByValue)
{
ProfileBase[] profileBase = null;
try
{
var serviceContext = SPServiceContext.Current;
var userProfileManager = new UserProfileManager(serviceContext);

profileBase = userProfileManager.Search(searchByValue);

return profileBase;
}
catch (Exception innerException)
{
throw innerException;
}
}



You can always cast the ProfileBase to a UserProfile:

UserProfile userProfile = (UserProfile)currentPB;



Good Luck ;)

5 comments:

  1. Boas! O que passas no value? Podes colocar um exemplo? Abraço =)

    ReplyDelete
  2. Viva. Passas, por exemplo, "Pedro" se quiseres pesquisar por user profiles que tenham o nome Pedro.

    ReplyDelete
  3. ok, estava a pensar se não seria por exemplo uma expressão...

    ReplyDelete
  4. The searchByValue is case sensitive, ie "mis" does not return words start with "Mis", is there any way to get the wild card search working? "*mis*"

    ReplyDelete
  5. Hi Wang Hao, sorry for the late response. I didn't need that kind of search, since I searched for the Profile of the current user ;)

    Have you tried the ProfileSearchManager Class:

    http://msdn.microsoft.com/en-us/library/ee582195.aspx

    ?

    ReplyDelete