Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
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 '12 at 18:41
add comment

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer
    
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 '12 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 '12 at 8:58
    
One way is to loop through all siteusers, but this solution is a performance killer ( –  Alexander Mar 31 '12 at 10:27
add comment

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

share|improve this answer
add comment

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?

share|improve this answer
    
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)". –  Dipesh Bhanani Oct 23 '12 at 8:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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