How i can get the NameId of an active user account if we have 2 users with the same email; one whihc is active and the other is inactive

john john Pter 1,160 Reputation points
2025-10-22T15:24:18.0533333+00:00

We have 2 users with the same email; one which is inactive and the other is active. now in SharePoint any user who access the site will be added to the SharePoint hidden list, now even if the user account is deleted from AD or get deactivated the user account will stay inside SharePoint hidden list forever. now the available info for this user inside SharePoint will be :-

  • loginname
  • email
  • sharepoint internal id
  • NameId

so we need to automate the removal of the old account from SharePoint. now i want to do the following:-

  1. Query Graph API, by sending the email address >> and get the NameId for the active account.

then i can query the duplicate users inside SharePoint and remove the one which dose snot match the above one. but how can call Graph API to get the NameId of the active user using the user email address?? the name Id seems to be different from the user ID inside AD it has this format:-

  • NameId: "1003**14a6****2f",
  • NameIdIssuer: "urn:federation:microsoftonline"

Thanks

Exchange Online
Exchange Online
A cloud-based service included in Microsoft 365, delivering scalable messaging and collaboration features with simplified management and automatic updates.
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Teddie-D 6,475 Reputation points Microsoft External Staff Moderator
    2025-10-23T01:30:01.9366667+00:00

    Hi @john john Pter 

    Thank you for posting your question in the Microsoft Q&A forum. 

    Microsoft Graph does not expose SharePoint’s internal NameId, as that value is specific to SharePoint's identity system and differs from the Azure AD object ID. However, you can still identify and remove duplicate site users by following this approach: 

    1.Use Microsoft Graph to identify the active Azure AD user by email:

    GET https://graph.microsoft.com/v1.0/users?$filter=mail eq '******@domain.com' 
    

    If mail is not populated, you can try: 

    GET https://graph.microsoft.com/v1.0/users?$filter=userPrincipalName eq '******@domain.com'
    

    From the response, extract the ID, mail, or userPrincipalName. Ensure accountEnabled = true and the user is not deleted. If multiple users match, keep only the enabled one. 

    2.Use the ensureUser endpoint to get the current site user object: 

    POST https://{tenant}.sharepoint.com/sites/{site}/_api/web/ensureUser
    Content-Type: application/json;odata=verbose
    
    {
      "logonName": "i:0#.f|membership|******@domain.com"
    }
    
    

    This returns the SharePoint user object with Id (SharePoint internal user id), LoginName, Email, and Title. You won’t get NameId here, but you don’t need it to decide what to delete, you can use this Id or LoginName. 

    3.List all site users that have the same email 

    GET https://{tenant}.sharepoint.com/sites/{site}/_api/web/siteusers?$filter=Email eq '******@domain.com'  
    Accept: application/json;odata=nometadata
    

     4.Keep the correct user and remove duplicates

    Retain the user whose LoginName matches the expected claims format: i:0#.f|membership|******@domain.com

    Remove others via removeById: 

    POST https://{tenant}.sharepoint.com/sites/{site}/_api/web/siteusers/removeById(123) 
    

    or removeByLoginName:

    POST https://{tenant}.sharepoint.com/sites/{site}/_api/web/siteusers/removeByLoginName(@v)?  @v='i%3A0%23.f%7Cmembership%7Colduser%40domain.com' 
    

    I hope this helps resolve your issue.


    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.  

    0 comments No comments

Your answer

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