Hey Friends I am trying to develop a web part in that I am fetching the user name.

But doing this I am getting the error that User cannot be found.

But the main thing is that in the line where I am getting this error has the same code written in the above line and it is passed through that and gives me the error in the bellow line.
Code :

string pc = ddlPrincipleConsultant.SelectedItem.Text;
string associateconsultant = "";
string domain = System.Environment.UserDomainName;
string authoby = ddltravelcost.SelectedItem.Text;
SPUser user = web.EnsureUser(pc); //It gives me proper user name    
SPUser authorizedby = web.EnsureUser(authoby);// Here I get the error

Here is the screenshot of the error.

Screenshot for the above problem

And I also tried with below code.

SPUser user = web.AllUsers[pc];
SPUser authorizedby = web.AllUsers[authoby];

But in this it gives me error in both. Both statement do not find the users.

Please Help me to solve this prolem..

5 Answers 5


You need to include the Domain name in EnsureUser - Domain\Username

  • Yes it Worked. But I have got the user name in first statement and in the second statement it throws an error. What is the reason of it..??? Mar 20, 2013 at 13:52
  • 1
    EnsureUser will look if the user is in SP, or, if the user hasn't accessed SP, will go to Active Directory to look up the user. It may be that in one of those two scenarios the domain isn't required? Or perhaps if the user has direct permissions to SP vs permissions based on membership in an AD group?
    – lgaud
    Mar 20, 2013 at 13:55
  • Yes it is true but if I am getting all my user in DDL from the Active Directory then also it is not finding users. But for the first line it find the user. and in second line it gave me error. You can see in screenshot. Mar 21, 2013 at 4:10
  • I want to know why this happened?? What is the reason behind this that first time it find the user and the second time it gave error. Mar 21, 2013 at 6:51

I have the same error. I noticed that the error occurs when the username has "." like in your screenshot. To be safe, you need to get the encoded claim value for the user and pass that into the EnsureUser method. Look these articles:

  1. First;
  2. Second.

Or you can just add i:0#.f|myprovider| to the username so it looks like i:0#.f|myprovider|myuser and pass the result into EnsureUser. In my case "myprovider" is the name of my custom membership provider.


Your code seems correct, probably the problem is somewhere else. Meanwhile, could you try this approach?

SPUser user = null;
SPUser authorizedby = null;
using (_web = web.Site.OpenWeb(web.ID))
    user = _web.EnsureUser(pc); 
using (_web = web.Site.OpenWeb(web.ID))
    authorizedby = _web.EnsureUser(authoby); 

This code is obviously over verbose, but maybe it will works.

  • No this solution not worked. Mar 20, 2013 at 9:18
  • Did you check that pc and authoby strings are really equal? What would happen if you try to Ensure authoby string first? Mar 20, 2013 at 9:54
  • they both the string values comes from the DropDownList. In that list I am storing my Application Users. Which are created by me. And that users, I am storing in Strings pc and authoby. Mar 20, 2013 at 10:01
  • I have just tested with the same user in both the DDL list. but user can select different users. and the same error occurs. Mar 20, 2013 at 10:02

I had to convert a possible Claims Authentication name format back to DOMAIN\USER format to run EnsureUser. Seems EnsureUser will work with the claims format for existing users that have accessed the site, but not new users.

This code sample gets the email address for a user by first checking if the provided name is in claim format and converts it to DOMAIN\USER format:

using Microsoft.SharePoint.Administration.Claims;

string emailAddress = string.Empty;
string userLogin = userName;
if (SPClaimProviderManager.IsEncodedClaim(userName))
  userLogin = SPClaimProviderManager.Local.ConvertClaimToIdentifier(userName);

SPUser user = web.EnsureUser(userLogin);
if (user != null)
  emailAddress = user.Email;

Try with below code. It will worked properly.

SPUser u = SPContext.Current.Web.Users[username];

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.