1

Is there a way to search profiles in MOSS from the object model? I need to search for profiles that have a certain value set on their profile and then perform some action for them.

I need to write some c# code that can search the profiles database and return the matching profiles. Basically,

List of Profiles = Select Profiles From Profile Store Where Profile Property Value = SomeValue

I'm trying to avoid the following:

 private IEnumerable<UserProfile> SearchProfiles(string value) {
        ServerContext serverContext = ServerContext.GetContext(SPContext.Current.Site);
        UserProfileManager profileManager = new UserProfileManager(serverContext);
        foreach (UserProfile profile in profileManager) {
            if ((string)profile["MyProp"].Value == value) {
                yield return profile;
            }
        }
    }
user160231
  • 109
  • 1
  • 3
  • 7

2 Answers2

1

This is possible using the FullTextSqlQuery class:

FullTextSqlQuery q = new FullTextSqlQuery(ServerContext.Current);
q.ResultTypes = ResultType.RelevantResults;
q.QueryText = "SELECT UserName, Email, PreferredName FROM SCOPE() WHERE \"scope\" = 'People' AND Department = 'IT'";

ResultTableCollection tables = q.Execute();
ResultTable results = tables[ResultType.RelevantResults];

This class allows you to query a specific scope (i.e. people) and filter them based on properties using the WHERE clause, which looks basically the same as a regular Sql Query.

To be able to search and filter on (custom) user profile properties, the profile property needs to have a mapping in the Shared Service Provider's metadata settings. Most out of the box user profile properties already have these, custom properties you have to add yourself.

More info on managed properties here.

Colin
  • 10,630
  • 28
  • 36
0

Two things:

  1. when running with elevated privileges we need to create a new SPSite object within the call and load the security context from there. DO NOT use the context obtained using SPContext.Current.Site.

    Hence:

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
     using (SPSite site = new SPSite("<your site url>"))
     {
      ServerContext context = ServerContext.GetContext(site);
    
      UserProfileManager profileManager = new
    
      UserProfileManager(context);
    
      foreach (UserProfile profile in profileManager)
      {
       // your code
      }
     }
    }
    
  2. make sure that the app pool account has appropriate user permissions in SSP. i.e. (use personal features, manage user profiles)

Himani
  • 1