none
Get Mail Id from user name using JavaScript RRS feed

  • Question

  • Hi Guys, 

    i have sharepoint list , which has names of employees , i wanna get their mail ID, 

    how can u do this using JAVA script , (the names are in 'Single line of text' column type)

    so please let me know how can i get the Mail ID using the Names

    Thanks in advance

    Wednesday, March 15, 2017 7:34 AM

All replies

  • Hi,

    You can achieve the same using REST api, Please have below function for your reference.

        function getMailID(listName, empName, empFieldInternalName) {
            $.ajax({
                url: _spPageContextInfo.webServerRelativeUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?$select=*&$filter=" + empFieldInternalName + " eq '" + empName + "'",
                method: "GET",
                headers: {
                    "Accept": "application/json;odata=verbose"
                },
                success: function(data) {
    
                    var result = data.d.results;
                    if (result.length > 0) {
                        // you will get the employee record here
                        // check item properties e.g. result[0].MailId;
                    }
                }
            });
        }

    Thanks.

    Wednesday, March 15, 2017 8:46 AM
  • Thanks for the Reply jayadeep,

    but i do not have any other fields other than EmployeeName which is of "Text" data type,

    now i wanna fetch Mail Id of each employee with the name in the list 

    Wednesday, March 15, 2017 9:10 AM
  • Can you please brief your requirement as much as possible for my understanding?

    Regards.

    Wednesday, March 15, 2017 9:47 AM
  • I have an list with one column called "Name" which is of type "Single line of text"

    this column consists of Employee names , 

    now i want to get all these Employees Mails ID 

    Wednesday, March 15, 2017 10:06 AM
  • I would suggest to add one more column to store employee email id and then you can retrieve the same using rest api as suggested. 

    I still don't understand purpose of having a list containing employee names only and that is too of single line of text. It should people and group field so that you can get email id from user properties.

    Regards.

    Wednesday, March 15, 2017 10:14 AM
  • Hi jayadeep , the thing is , i had an excel with this names , and i imported it to sharepoint list , the client is asking to fetch the mail Id of each user for this list 
    Wednesday, March 15, 2017 10:16 AM
  • Hi,

    As Jaydeep mentioned, you have to modify the Employee Name column data type to People Picker and then it is easier to get the Email ID from it.


    Cheers,
    Maruthu
    Please click the 'Mark as Answer' if this post solves your problem or "Vote As Helpful" if it was useful! :)
    Explore the SharePoint

    Thursday, March 16, 2017 1:22 AM
  • Hi,

    We can jQuery Ajax with REST API to achieve it.

    Use "/_api/web/lists/getbytitle('listname')/items" to get all the list items and use "/_api/web/siteusers" to get all the site users.

    The following code for your reference, modify the "listname" in the code below and add the code into a content editor web part.

    <script src="//code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(function () {
    	var listname = "UserList";
    	var url =  _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listname + "')/items?$select=Name";
    	$.ajax({
    		url: url,
    		method: "GET",
    		headers: { "Accept": "application/json; odata=verbose" },
    		success: function (data) {		 
    			var items = data.d.results;
    			var siteUsers=getSiteUsers();
    			var table="<table border='1'><th>User Name</th><th>User Email</th>";
    			for(var i = 0; i < items.length;i++) {
    				table+="<tr><td>"+items[i].Name+"</td><td>"
    				for(var j = 0; j < siteUsers.length;j++) {
    					if(items[i].Name==siteUsers[j].Title){
    						table+=siteUsers[j].Email;
    					}
    				}
    				table+="</td></tr>"
    			}
    			table+="</table>";
    			$("#UserListWithEmail").html(table);
    		},
    		error: function (data) {		
    		}
    	});
    });
    function getSiteUsers(){
    	var results;
    	var url =  _spPageContextInfo.webAbsoluteUrl + "/_api/web/siteusers?$select=Title,Email";
    	$.ajax({
    		url: url,
    		method: "GET",
    		async: false,
    		headers: { "Accept": "application/json; odata=verbose" },
    		success: function (data) {		 
    			results = data.d.results;
    		},
    		error: function (data) {
    		}
    	});
    	return results;
    }
    </script>
    <div id="UserListWithEmail">
    </div>
    

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, March 16, 2017 2:51 AM
    Moderator
  • Hi,

    Is there any update?

    If the post helps you, you can mark it as answer to help other community members to find the solution quickly.

    Have a nice day!


    Please click the "Mark as Answer" button if this post solves your problem or "Vote As helpful if it was helpful! :)
    Explore the SharePoint

    Tuesday, June 6, 2017 6:15 PM