I am accessing the Author name from the list item. Even if the user has been deleted from AD, even then he still appears in list item history.

I have used the following method for development:

var author = new SPFieldUserValue(listItem.Web, listItem[SPBuiltInFieldId.Author].ToString());

And then using it to set different permissions, and here I'm using:


The code works fine for existing users but when it encounters a user who has been delete from AD (but still visible in SharePoint) it throws an error.

What is the best way to check if the user exists/is still active? Please suggest.

if (author.User != null)
     //do something..
else {
     //do something else..

Use web.EnsureUser method inside try/catch block to check user by login

EnsureUser creates SharePoint user if it does not exist, BUT...

1) Users from Author field already exists as SharePoint users and don't be created again.

2) If Author user does not exists in AD, EU generates exception.


You would check if the user exists in AD or not. A code sample here: http://kancharla-sharepoint.blogspot.in/2012/06/c-code-to-check-user-exists-in-active.html

using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "DomainName"))
using (UserPrincipal user = UserPrincipal.FindByIdentity(context, "LoginName"))
 bool userExists = (user != null);
 if (userExists)
   Console.WriteLine("User exists");
  Console.WriteLine("User doesn't exist");
  • Why is this better than my if-else? – RangeRover Sep 28 '15 at 6:17
  • And does this find users who are inactivated/disabled in AD? – RangeRover Sep 28 '15 at 6:18
  • Your if/else won't check in AD. That'll just check in the current site collection / web if the user exists. This checks for the user in AD. Alexey's answer is also valid. EnsureUser would throw an exception if the user is not found in AD. This one wouldn't throw an exception since you are finding the user in AD. And, no this wouldn't find inactive / disabled users in AD. – Akhoy Sep 28 '15 at 6:42
  • I might be wrong here. Check this: sharepoint.stackexchange.com/questions/86145/…. According to this, Alexey's answer might be right.This would check for the user but if you want to get the user, you would need to look at ensureuser. – Akhoy Sep 28 '15 at 6:55
  • Right, and I don't want to throw an error/add user again. I just want to check if the user exist and if not I want to add permissions to other user/group. – RangeRover Sep 28 '15 at 7:15
var SPuser = web.AllUsers.Cast<SPUser>().FirstOrDefault(user => user.LoginName == author.LoginName);
if (SPuser != null) 
//Do Something here
  • Thanks, but if I just go with: if (author.User != null) like above I get the same result? How is it with performance when web.Allusers is called (this will be used often) and our intranet contains almost a thousand users? – RangeRover Sep 28 '15 at 8:27
  • Ya, this has performance impact when compared to web.AllUsers[userName]. But, you'll get into the loop only if the SPUser variable has some user value in it. Unlike web.AllUsers[userName] which would throw an exception – Shishirchandra Prasad Sep 28 '15 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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