0

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");
    }
});

3 Answers 3

0

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:

  1. Get all properties of current user:

http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties

  1. Get single property of current user:

http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties/Email

OR

http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=Email

  1. Get Multiple Properties for the current user:

http://siteurl/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=Email,AccountName

1

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.

0

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");
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.