4

How to convert this display name to SPUser type.

My input is Sivakumar, Piratheban (ext)(domain\user)

2
  • If it is a string you can user SPUser user=web.EnsureUser(input).
    – user13408
    Commented Apr 30, 2015 at 7:12
  • 1
    The EnsureUser method needs the login name. The string 'Sivakumar, Piratheban (ext)(domain\user)' would not fit into that. One should first parse out the login name from this kind of display name pattern.
    – pholpar
    Commented Apr 30, 2015 at 7:51

2 Answers 2

3

There is no direct convertion but you can use a regular expression to pull out the login name and then call EnsureUser on that.

var login = Regex.Match(input, @"\((?<login>[^\(\\]+\\[^\(\\]+)\)$", RegexOptions.ExplicitCapture).Groups["login"].Value;
var user = web.EnsureUser(login);
2

You could extract login name from input string using String.Split Method and then initialize a user:

var loginName = input.Split('(', ')')[3];
var user = web.EnsureUser(loginName);
2
  • 2
    Living on the edge with not checking if you actually get four positions in the array resulting from the split ;) Commented May 2, 2015 at 11:09
  • 2
    I agree, no matter what input string will contain, the checking at least for the length of resulting array is a must ;) Commented May 2, 2015 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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