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
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.
- Authenticated user logs into site.
- 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.
-
1
-
1The 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
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.