How do I get Sharepoint username from display name programmatically? Is there any specific method?

  • 6
    The problem with a display name is that it is not doomed to be unique (I mean you can easily have two persons called Adam Anderson or something). So you should really try to avoid building your code on a logic of display name – Robert Lindgren May 25 '15 at 7:44

While you should really keep in mind what Robert suggested in the above comment, if all you need is to try to resolve a name to a SharePoint user like the people picker does, you should have a look at the SPUtility class methods

SPUtility.SearchPrincipals and SPUtility.ResolvePrincipal

for example:

bool hasReachedTheMaxCount= false;
List<SPPrincipalInfo> matchingUsers= SPUtility.SearchPrincipals(targetWeb, "search string", SPPrincipalType.User, SPPrincipalSource.All, null, 100, out reachedMaxCount);

Should return all principals (including users) with a name matching the given string.

Be wary that the documentation on these two methods is pretty bad, so you may need to do some experiments before finding the appropriate filters and flag masks to use to have only users returned.

  • Is there a CSOM API for the same for SP2010 or is there a way to do this with WebService – Ravi Khambhati Apr 13 '18 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.