How to get users email address using SharePoint REST API
-
17-02-2021 - |
Domanda
I am using the following code to get the current logged in user to a SP page. I'm only able to get the Title. How can I get the email address as well? Thank you.
// Get current user
jQuery.ajax({
url: "https://.../_api/web/currentuser?$select=Title,User/ID,User/EMail&$expand=User",
type: "GET",
headers: {"Accept": "application/json;odata=verbose","Content-Type": "application/json;odata=verbose"},
success: function(data) {
username = data.d.Title;
console.log(data.d);
console.log("user name: " + username);
document.getElementById("user").innerHTML = username;
},
error: function(data) {
console.log("Error occurred trying to get user id");
}
});
Soluzione
If you want to retrieve email by REST API in the way as you posted you can simply change url
parameter to https://.../_api/web/currentuser?$select=Title,Email
So your code will be
// Get current user
jQuery.ajax({
url: "https://.../_api/web/currentuser?$select=Title,Email",
type: "GET",
headers: {"Accept": "application/json;odata=verbose","Content-Type": "application/json;odata=verbose"},
success: function(data) {
username = data.d.Title;
console.log(data.d);
console.log("user name: " + username);
console.log("user email: " + data.d.Email);
document.getElementById("user").innerHTML = username;
},
error: function(data) {
console.log("Error occurred trying to get user id");
}
});
Also, there are a lot of another ways to retrieve current user properties:
- Get all properties of current user:
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties
- Get single property of current user:
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties/Email
OR
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=Email
- Get Multiple Properties for the current user:
http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=Email,AccountName
Altri suggerimenti
You don't need to use the REST API to get the current user's display name or email address. You can get them from a JavaScript variable named _spPageContextInfo that is injected into each Classic SharePoint page. Use _spPageContextInfo.userDisplayName to get the currect user's name and _spPageContextInfo.userEmail to get their email address.
You can get lots of other valuable information from _spPageContextInfo. See Using _spPageContextInfo to Determine the Current SharePoint Context in Script for more information.
You need to use the Email only. No need to use User/EMail.
Try below.
// Get current user
jQuery.ajax({
url: "https://.../_api/web/currentuser?$select=Title,Email",
type: "GET",
headers: {"Accept": "application/json;odata=verbose","Content-Type": "application/json;odata=verbose"},
success: function(data) {
username = data.d.Title;
console.log(data.d);
console.log("user name: " + username);
console.log("user Email: " + data.d.Email);
document.getElementById("user").innerHTML = username;
},
error: function(data) {
console.log("Error occurred trying to get user id");
}
});