0

I have a field called Employee and it's a person and group field. How can i get the users email address if i have the display name or the user id. Any tip/help will be appreciated

See my snippet code below:-

<script type="text/javascript">
   function StartersItems() {
    var context = new SP.ClientContext.get_current();
    var web = context.get_web();
    var list = web.get_lists().getByTitle("Starters");
    var camlQuery = new SP.CamlQuery();

    var camlText = "<View><Query></Query></View>";

    camlQuery.set_viewXml(camlText);
    this.collListItem5 = list.getItems(camlQuery);

    context.load(collListItem5);
    context.executeQueryAsync(getNewStartersSuccess,getItemsFail);
}

function getNewStartersSuccess(sender,args){
    var listEnumerator = collListItem5.getEnumerator();

    i = 1;
    var newStarters = [];

    var newStartershtml= "";

    Startershtml+='<ul id="newStarterContainer">';


    while (listEnumerator.moveNext()) {

        var listItem = listEnumerator.get_current();

        //Employee is a person and Group field
        var employeeName = listItem.get_item("Employee").get_lookupValue();
        var employeeID = listItem.get_item("Employee").get_lookupId();

        //Can return these values no issue
        //console.log(employeeName);
        //console.log(employeeID);

        //Need help with a function to return email address here or use any of the Ids above
        FunctionToReturnEmailAddress(listItem.get_item("ID"));




    }
function getItemsFail(sender,args){
   console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

}

    </script>
  • Try using var user = web.ensureUser(employeeName); var email = user.get_email(); – Ganesh Sanap Sep 17 '19 at 7:52
1
var context = new SP.ClientContext.get_current();
var user = context.get_web().get_siteUsers().getById(userid);
context.load(user);
context.executeQueryAsync(
    Function.createDelegate(null, ensureUserSuccess),
    Function.createDelegate(null, onFail)
);

function ensureUserSuccess() {
    var email = user.get_email();
}

function onFail(){
}
| improve this answer | |
0

To get the email address or login name of user, you need to use web.ensureUser() method and then get the email or login name as below:

Example:

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getItemFromList, "sp.js");
function getItemFromList(){
    var clientContext = new SP.ClientContext();
    var item = clientContext.get_web().get_lists().getByTitle("MyTasks").getItemById(1);
    clientContext.load(item);
    clientContext.executeQueryAsync(
        function(){ 
            // successfully retrieved value from list item
            var assigned = item.get_item("AssignedTo");
            if(assigned.length>0){
                var user = clientContext.get_web().ensureUser(assigned[0].get_lookupValue());
                clientContext.load(user);
                clientContext.executeQueryAsync(
                    function(){ 
                        // successfully ensured user from user name
                        var email = user.get_email();
                        var login = user.get_loginName();
                        var displayName = user.get_title();
                        alert("User LoginName: " + login + "\nUser Email: " + email + "\nUser Display Name: " + displayName);
                    },function(sender,args){ // on error
                        alert(args.get_message());
                    }
                );
            }          
        },
        function(sender,args){ // on error
            alert(args.get_message());
        }
    );
}
</script>

Reference: Retrieve displayName and email from person or group list field in SharePoint .

Solution for You:

So for your case you can use your function like this:

function FunctionToReturnEmailAddress(employeeName) {
    var user = clientContext.get_web().ensureUser(employeeName);
    clientContext.load(user);

    clientContext.executeQueryAsync(
        function(){ 
            // successfully ensured user from user name
            var email = user.get_email();
            var login = user.get_loginName();
            var displayName = user.get_title();
            alert("User LoginName: " + login + "\nUser Email: " + email + "\nUser Display Name: " + displayName);
        },function(sender,args){ // on error
            alert(args.get_message());
        }
    );
}

Usage:

FunctionToReturnEmailAddress(employeeName);
| improve this answer | |
  • Thanks Ganesh but how can i return the email value in executeQueryAsync? – naijacoder Sep 18 '19 at 0:05
  • Why do you want to return it? Either you can create a new function to write the remaining functionality and pass an email to that function or you can try using return to return the email and catch the result where the FunctionToReturnEmailAddress is called. – Ganesh Sanap Sep 18 '19 at 2:31
  • Thats exactly what im asking how to use return in the function you provided.It seems i cant use return with an async.Hope this makes sense... – naijacoder Sep 18 '19 at 8:44
  • I need to return the email as string with thia function – naijacoder Sep 18 '19 at 8:48
  • Can you create a new function and call this function after the alert() and pass an email as a parameter to the function?? Or you can use promise pattern with as JSOM as mentioned here or here. – Ganesh Sanap Sep 18 '19 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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