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

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..

share|improve this question
add comment

4 Answers 4

up vote 2 down vote accepted

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

share|improve this answer
    
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..??? –  Rahul Gokani Mar 20 '13 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 '13 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. –  Rahul Gokani Mar 21 '13 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. –  Rahul Gokani Mar 21 '13 at 6:51
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
No this solution not worked. –  Rahul Gokani Mar 20 '13 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? –  Dmitriy Inozemtsev Mar 20 '13 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. –  Rahul Gokani Mar 20 '13 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. –  Rahul Gokani Mar 20 '13 at 10:02
add comment

Try with below code. It will worked properly.

SPUser u = SPContext.Current.Web.Users[username];
share|improve this answer
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.