2

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?

2 Answers 2

2

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.

2
  • 1
    I am also adding this through people picker. Aug 24, 2014 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. Aug 24, 2014 at 10:25
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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