Email address is incorrect for user in SharePoint

In the process of migrating from SharePoint 2010 to 2016 and ran into a small problem.

When trying to get the email property from the SPUser class, it returned a value of domain\userName. Clearly, this is not correct and caused some other issues.

Sample code

1
2
3
$userEnsure = $web.EnsureUser("domain\yourNameHere")
write-host $userEnsure.Email

Running this returned domain\yourNameHere, when it should have returned yourname@domain.com.

Navigate to Central Admin, then cruise over to your User Profile Service. Once there, run a full synchronization.
Profile Service –> Synchronization –> Start Profile Synchronization –> Start Full Synchronization

Run the PowerShell script again, and it will return the correct data.

Same idea as above but using the SharePoint ClientContext.

1
2
3
4
5
6
7
8
9
10
using (ClientContext clientContext = new ClientContext("https://webapp.taco/toppings/cheese"))
 {
     Web web = clientContext.Web;
 
     clientContext.Load(web);
     clientContext.Load(web.CurrentUser);
     clientContext.ExecuteQuery();
 
     var userEmail = web.CurrentUser.Email;
}

Leave a Reply

Your email address will not be published. Required fields are marked *