4

How to convert this display name to SPUser type.

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

  • If it is a string you can user SPUser user=web.EnsureUser(input). – user13408 Apr 30 '15 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 Apr 30 '15 at 7:51
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
    Living on the edge with not checking if you actually get four positions in the array resulting from the split ;) – Robert Lindgren May 2 '15 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 ;) – Vadim Gremyachev May 2 '15 at 11:17

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.