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 tried to update Person column with C#. NT AUTHORITY\authenticated users in the site have read permission at site site level. Sometimes when the code try to find user by login id from web.AllUsers["login id"] it throws an SPException "User cannot be found". How can I fix this mess?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Adding all authenticated users does not mean they are getting to the site. Yes, they have permission to site but if you check the User Information List of the site, they cannot be found there.

When you call web.AllUsers, it checks in the user information list. Since the user is not there, it can cause error. There are scenarios where SharePoint put into this list by itself.

  1. Authenticated user logs into site.
  2. Add user through OOTB people picker.

The solution to this is using the web.EnsureUser(@"domain\username"). This will first check whether the user is there in the User Information List and if not, the user will be added to the list and then return the SPUser object.

share|improve this answer
1  
I am also adding this through people picker. –  Angelo Perez Aug 24 at 10:22
1  
The Out Of The Box people picker is different to the one that you are using. Also in that forms, the implementations are written by SharePoint Team. So they should have added it. –  Malin De Silva Aug 24 at 10:25

Ensure that the user you are trying to add exist in the web site. You can make use of EnsureUser() function of the SPWeb object.

You may write your code as:

SPWeb spWeb = SPContext.Current.Web;
SPUser spUser = spWeb.EnsureUser(@"DOMAIN\username");

On MSDN article you can learn more about EnsureUser() function.

Hope this helps.

share|improve this answer

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.