2

I have a string "System Account", how can I convert the string into SPUser?

I've tried SPContext.Current.Web.EnsureUser("System Account") and also web.AllUser["System Account"] but hit error.

Please help, thank you in advanced.

1
  • I've tried SPWeb.AllUsers.GetByID(userID).ID.ToString() and it solved my issue, I should have use userID instead of user name in string
    – sams5817
    Apr 1, 2012 at 18:41

3 Answers 3

4

You can retrieve the system account by SPSite property

SPContext.Current.Site.SystemAccount

Basicaly EnsureUser method uses logon name (Domain\User_Alias) of the user.

3
  • thanks for your reply, what if I do not have Domain\User_Alias? I only have the welcome name in string format? is it still possible I validate and convert into SPUser?
    – sams5817
    Mar 31, 2012 at 8:50
  • I do not sure, but welcom name is not an unique name... if it is true, i think that it is not possible.
    – Alexander
    Mar 31, 2012 at 8:58
  • One way is to loop through all siteusers, but this solution is a performance killer (
    – Alexander
    Mar 31, 2012 at 10:27
3

i guess you are looking for some api to resolve inputs. try this utility method:

Microsoft.SharePoint.Utilities.SPUtility.ResolvePrincipal

it will return a SPPrincipal object, witch contains a "LoginName" property. then you can use SPWeb.EnsureUser(loginName) to get a SPUser object.

more infomation about this method: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.utilities.sputility.resolveprincipal.aspx

3

Try this, should work

string strUser = “Shiva Bala”;

//(myWeb is your SPWeb object)

SPFieldUserValue flduserVal = new SPFieldUserValue(myWeb, strUser);

SPUser myUser = flduserVal.User;

plus can someone tell me how to do it if there is a domain user?

1
  • You can not pass the display name as argument to SPFieldUsesrValue constructor. This requires item value (e.g. "12#;Shiva Bala"). With respect to question, Basically, one need to pass Logon Name as parameter in EnsureUser method. And for fetching user, one should use "SPWeb.AllUsers.GetByID(loginName)". Oct 23, 2012 at 8:25

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.