Reputation: 1105
I have function like this:
function sendEmail() {
var appweburl = _spPageContextInfo.webAbsoluteUrl;
var urlTemplate = appweburl + "/_api/SP.Utilities.Utility.SendEmail";
var fieldName = "Notificar a";
var body = "Descripción";
var _body = $("textarea[title^='" + body + "']");
var fullURL = window.location.href
var url = new URL(fullURL );
var ID = url.searchParams.get("ID");
var _PeoplePicker = $("div[title='" + fieldName + "']");
var emailSpan = $(_PeoplePicker).find('.sp-peoplepicker-userSpan');
var _sid ="";
var arrEmailID = [];
if (emailSpan.length > 0) {
$(emailSpan).each((index,el) => {
arrEmailID.push(`'${$(el).attr('sid').split("|")[2]}'`);
});
} else {
alert("null");
}
var fieldUsuario = _sid;
var fromMail = '[email protected]';
var subject = 'Tarea Editada: ' + 'ID: ' +ID + ' ' + _body.val();
var body = 'Se ha editado la tarea: '+ 'ID: ' + ID+ ' ' + 'Descripción: ' + _body.val() ;
$.ajax({
contentType: 'application/json',
url: urlTemplate,
type: "POST",
data: JSON.stringify({
'properties': {
'__metadata': { 'type': 'SP.Utilities.EmailProperties' },
'From': fromMail,
'To': { 'results': [arrEmailID] },
'Body': body,
'Subject': subject
}
}
),
headers: {
"Accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
var result = data.d.results;
},
error: function (err) {
alert(JSON.stringify(err));
}
});
}
As you can see I have var arrEmailID = [];
it populates foreach emailSpan
I receive, so at the final of the day I have something like this:
var emailSpan = $('#_PeoplePicker').find('.sp-peoplepicker-userSpan'),
arrEmailID = [];
$(emailSpan).each((index,el) => {
arrEmailID.push(`'${$(el).attr('sid').split("|")[2]}'`);
});
console.log(arrEmailID);
.as-console-wrapper {max-height: 100% !important;top: 0;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="_PeoplePicker">
<span sid="a|B|[email protected]" class="sp-peoplepicker-userSpan"></span>
<span sid="a|B|[email protected]" class="sp-peoplepicker-userSpan"></span>
<span sid="a|B|[email protected]" class="sp-peoplepicker-userSpan"></span>
<span sid="a|B|[email protected]" class="sp-peoplepicker-userSpan"></span>
</div>
I want to know if its possible to execute ajax call foreach item arrEmailID
and use that item in To': { 'results': [arrEmailID] },
Upvotes: 1
Views: 290
Reputation: 2182
I think you're looking for jQuery.when
.
Pseudocode to do what you'd like would be along the lines of:
jQuery.when(emails.map(email => {
// your function details
return jQuery.ajax({
// your ajax configuration
});
})
.then((results) => {
// handle the results here
});
Upvotes: 1