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.