Get email address from user's full name

Sherazad Ahmed 411 Reputation points
2022-03-30T20:54:07.197+00:00

Hello,

I have some custom jquery coding in newform.aspx of a list and would like to look up the user's email address in SharePoint using the user's full name (First and Last Name). Can someone point me to to code to achieve this?

The environment is SharePoint 2016 on prem.
thanks,

Microsoft 365 and Office SharePoint Development
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,251 Reputation points
    2022-03-31T02:55:27.023+00:00

    Hi @Sherazad Ahmed ,
    According to my research and testing , we can use jQuery Ajax with REST API to get email address.

    I tried to create a list with the 'username' column and then get the email address from username using RESI API;

    Here is an example you can refer to, hope it can help you :

    <div id="UserWithEmail"></div>  
    <script src="https://code.jquery.com/jquery-2.2.4.js" type="text/javascript"></script>  
    <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>  
    <script type="text/javascript" src="/_layouts/15/sp.js"></script>  
    <script src="https://MyServer/sites/SiteCollection/style library/js/ScriptFile.js"></script>  
    <script src="//code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>  
    <script type="text/javascript">  
    $(function () {  
    	var listname = "user";  
    	var url =  _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listname + "')/items?$select=username";  
    	$.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].username+"</td><td>"  
    				for(var j = 0; j < siteUsers.length;j++) {  
    					if(items[i].username==siteUsers[j].Title){  
    						table+=siteUsers[j].Email;  
    					}  
    				}  
    				table+="</td></tr>"  
    			}  
    			table+="</table>";  
    			$("#UserWithEmail").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>  
    

    188519-image.png

    188594-image.png


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.