Determine Email of the person in the people field using JSOM or REST API

Hi,

I have to build below HTML dynamically. The source to build this is a sharepoint list that has a people field in it.

Can I use JSOM or REST API to do this? if so, can anyone help with some examples?

<a href='mailto:person1@domain.com,person2@domain.com'>Write email</a>


November 25th, 2014 10:13pm

Hi,

You can use SPServices to get mail id from people picker field as follows :

$().SPServices({ operation: "GetListItems", async: false, listName: "Assignees", webURL: "https://col.wow.telenor.com/sites/go/",
            CAMLViewFields: "<ViewFields Properties='True'/>",
            CAMLQuery: "",
            CAMLQueryOptions: "<QueryOptions><ExpandUserField>True</ExpandUserField></QueryOptions>",
            completefunc: function (xData,Status) {

                $(xData.responseXML).SPFilterNode("z:row").each(function () {

                    try {
                    //ows_Name1 is a field of type "People or Group" the after adding CAMLQueryOptions this field returns all the fields
                    // propeties of user i.e. Displayname,ID,email id, domain login, sip ID etc all separate by #
                        var title = $(this).attr("ows_Name1"); 
                    // Splitting the resultant string with # give you string array of all the properties. In my case email ID is at 
                    // index 3.
                        var userEmail = userText.split('#')[3];
                    // Below line is added to remove the trailing "," from email id
                        userEmail = userEmail.substring(0,userEmail.indexOf(','));

                    }
                    catch (e) {
                        alert('Exception: ' + e.message);
                    }
                });
            }
        });

ExpandUserField parameter is used to expand fields within people picker field. You can use then append it to the string and get the required HTML.

Source: http://stackoverflow.com/questions/13379403/get-email-id-of-user-in-spservices

Hope it helps!

Thanks,

Avni Bhatt

Free Windows Admin Tool Kit Click here and download it now
November 26th, 2014 4:30am

Hi Avni,

Thanks for the response. However, I think this is possible with JSOM and REST API as well. I prefer to use more SharePoint native Libraries to do this.

Thanks

November 26th, 2014 7:30pm

Hi,

You can use User Information list (hidden) to get the specific user details, here you need email id. Try like this

 $.ajax({
	url: "https://<site URL>/_vti_bin/listdata.svc/UserInformationList?$filter=startswith(Title,'<Name of the person>')",
	method: "POST",
	headers: {
	"accept": "application/json;odata=verbose",
	},
	success: getUserEmailSuccess,
	error: getUserEmailError
  });
  
  
  function getUserEmailSuccess(data){
	var requiredEmailID = data.d.results[0].WorkEmail;
	//bind requiredEmailID value to your anchor tag
  }
  
  function getUserEmailError(){
  }

.

Free Windows Admin Tool Kit Click here and download it now
November 26th, 2014 8:22pm

I think, I did not explain my case correctly. 
Here is my scenario. A SharePoint list on the site with two fields. One field is "Title" and the Other field is an Employee field with user data type.

Now, I want to send email to all the users in this list by one click. i.e launch outlook client with emailids of all the users in the Employee field.

Can this be done without refering to the userinformationlookup list?

November 26th, 2014 10:11pm

Hi,

The following code for your reference:

<script src="http://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        var array=new Array();
        var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('CustomList')/items?$select=EmployeeId";

        // execute AJAX request
        $.ajax({
            url: requestUri,
            type: "GET",
            headers: { "ACCEPT": "application/json;odata=verbose" },
            success: function (data) {
                var dataResults = data.d.results;
                for(var i=0;i<dataResults.length;i++){
                    if(dataResults[i].EmployeeId!=null){
                        for(var j=0;j<dataResults[i].EmployeeId.results.length;j++){                           
                            array.push(dataResults[i].EmployeeId.results[j]);
                        }
                    }
                }

                //remove the duplicated id
                var employeeIds=$.unique(array);

                //get the employee email by user id
                var emails=getEmailsByIDs(employeeIds);

                //add to send mail div
                $("#SendMail").append("<a href='mailto:"+emails+"'>Send Email</a>");

            },
            error: function () {
                //console.log("Failed to get details");
            }
        });

    });
    function getEmailsByIDs(employeeIds){

        var emails=new Array();

        var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/siteusers?$select=Id,Email";

        // execute AJAX request
        $.ajax({
            url: requestUri,
            type: "GET",
            headers: { "ACCEPT": "application/json;odata=verbose" },
            async: false,
            success: function (data) {
                var dataResults=data.d.results;
                for(var i=0;i<dataResults.length;i++){
                    for(var j=0;j<employeeIds.length;j++){
                        if(dataResults[i].Id!=null&&dataResults[i].Id==employeeIds[j]&&dataResults[i].Email!=null){
                            emails.push(dataResults[i].Email);
                        }
                    }
                }
            },
            error: function () {
                //console.log("Failed to get details");
            }
        });
        return emails;
    }

</script>
<div id="SendMail">
</div>

Thanks,
Dennis Guo
TechNet Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.co

November 27th, 2014 2:57am

Your making the two ajax to get the email id for people field. Is there method to get the Email Id in single ajax call?
April 24th, 2015 3:15am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics