When looking up the user in Azure AD, returns a Microsoft.SharePoint.Client.Server Exception saying user could not be found for an Active AAD user.

GuptaPayal-9417 20 Reputation points
2023-08-11T04:31:11.4266667+00:00

I am trying to validate a user using ClientContext.Web.EnsureUser(EmailID). It is unable to retrieve the user details in the first attempt for some users but fetches the details in the second attempt. Please help me with this, why is it happening for some users in 2 attempts.

I tried with at least 100 new users, and a few of them are not getting added in first attempt saying user could not be found although it is an active user in AAD. This should not have happened.

I am using the below code snippet:

ClientContext ctx=new ClientContext(url)

var user=ctx.Web.EnsureUser(EmailID);

ctx.Load(user);

ctx.ExecuteQuery();

The variable user does not have fields like LoginName initialized for the first time.

But when we rerun the code again for the same user, the variable user has fields like LoginName and ID initialized. Although the user is active, why is the user not getting processed in the first try.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,560 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,838 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
21,352 questions
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 34,976 Reputation points Microsoft Vendor
    2023-08-11T06:14:56.8566667+00:00

    Hi @PAYAL GUPTA,

    Ensure user method in SharePoint Online checks if the given user account is valid and then adds the user object to the “User Information List”. And when you grant permission to an Active Directory group, the group will be added to the user information list but users in the group will not be added until they access the site collection.

    These users will not exist in the User Information List before you excute ensure user method. As aworkaround we can grant permission to users instead of groups then they will be added to user information list without login.

    References:
    https://social.technet.microsoft.com/Forums/en-US/3bf1d0f4-4b6e-449b-ab28-8e69ec26cccd/how-to-add-users-automatically-to-users-information-list-in-sharepoint-2013-without-manual-work?forum=sharepointadmin
    https://sharepointdiv.wordpress.com/2018/04/09/user-information-list-in-sharepoint-all-details/


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.