Expression Evaluation Time: ms / Round trip: ms
How to Search for Users in a SharePoint 2010 Claims-Based Authentication Environment?
29. Nov
2011
by Stefan Category:
Technical
0

Do you need to programmatically search for users in a claims-based authentication enabled SharePoint 2010 environment? SharePoint 2010 has an API to do that. The class SPClaimProviderOperations provides methods to search (and lookup/resolve) users within the available claim providers. Let me show you how to do this with the following example:

Uri context = new Uri(SPContext.Current.Site.Url);
string searchText = "john";
SPClaimProviderOperationOptions mode = SPClaimProviderOperationOptions.None;
string[] providerNames = null;
string[] entityTypes = new [] { "User" };
int resultLimit = 10;

SPClaimProviderOperations.Search(
  context,
  mode, 
  providerNames,
  entityTypes,
  searchText, 
  resultLimit);
  • context:
    Each web application zone has a set of claim providers. The context points to a zone and therefore specifies the claim providers that should be used.

  • mode:
    The mode can be used to manipulate the set of claim providers. None uses the default settings. See SPClaimProviderOperationOptions Enumeration for more information.

  • providerNames:
    Specifies an explicit set of claim providers. null uses the default settings.

  • entityTypes: Specifies the type of entities that should be returned. You can use the method SPClaimEntityTypes.ConvertFromPrincipalType() to convert a SPPrincipalType enum to entity types.

The parameters searchText and resultLimit should be self explanatory.

The Search method returns an array of type SPProviderHierarchyTree[]. Use the member EntityData of each SPProviderHierarchyTree to access the results per provider:

SPProviderHierarchyTree[] result = SPClaimProviderOperations.Search(/*...*/);
IEnumerable<PickerEntity> entities = result.SelectMany(t => t.EntityData);

foreach (PickerEntity entity in entities)
{
  string loginName = entity.Claim.Value;
  string displayName = entity.DisplayText;
  string claimsEncodedLoginName = entity.Key; 
}

In order to use a starts-with search you have to ensure that the PeoplePickerWildcards section is defined in the web.config:

<PeoplePickerWildcards>
  <clear />
  <add key="AspNetSqlMembershipProvider" value="%" />
  <add key="TheMembershipProviderName" value="%" />
</PeoplePickerWildcards>
Tags: Claims
Comments
ABOUT

This blog is about technical and non-technical aspects of the product MatchPoint and other SharePoint topics.

If you would like to post an article or if you have an idea for a post, please contact us.

ARCHIVE
COMMENTS
Matthias Weibel
09.04.2018 01:12
Link is updated and works now. | Goto Post
Dhanabalan
09.04.2018 12:21
Link doesn't work. Could anyone explain what does... | Goto Post
matthiaszbrun
14.03.2018 02:05
Hi Markus
We I use the config for SiteCollectionSe... | Goto Post
Reto Jeger
04.10.2017 09:15
Hello Reiner,
Thanks for pointing out the missing ... | Goto Post
rganser
29.09.2017 09:56
Hi, I downloaded the ZIP-file for MatchPoint Versi... | Goto Post