Answered by:
Custom Action to e-mail Item

Question
-
Is there any way to add a CEWP or other part to add ability to e-mail a list item? From what I see is that you need Visual Studios in order to do so. Can someone please assist?
Lee Mossolle
Tuesday, August 22, 2017 1:47 PM
Answers
-
Hi,
Raghava had pointed the solution, I just provide the tested sample code for your reference.
<script src="https://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script> <script type="text/javascript"> function triggerEmail() { var ctx = SP.ClientContext.get_current(); var listID = SP.ListOperation.Selection.getSelectedList(); var targetList = ctx.get_web().get_lists().getById(listID); var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx); for (var i = 0; i < selectedItems.length; i++) { var itemId = selectedItems[i].id; var item = targetList.getItemById(itemId); ctx.load(targetList, 'DefaultDisplayFormUrl'); ctx.load(item); ctx.executeQueryAsync( Function.createDelegate(this, function () { var itemTitle = item.get_item('Title'); var body = targetList.get_defaultDisplayFormUrl()+'?ID=' + itemId; sendMail('Administrator@contoso.com', ['user1@contoso.com'], 'CustomEmail' + itemTitle, 'item:' + body).done(function (response) { alert("Mail Sent."); }).fail(function (a,b,c) { alert(a.responseText); }); console.log(itemId); }), Function.createDelegate(this, function () { alert('error'); }) ); } } function sendMail(from,toList, subject, mailContent) { var restUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.Utilities.Utility.SendEmail", restHeaders = { "Accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "Content-Type": "application/json;odata=verbose" }, mailObject = { 'properties': { '__metadata': { 'type': 'SP.Utilities.EmailProperties' }, 'From': from, 'To': { 'results': toList }, 'Subject': subject, 'Body': mailContent, "AdditionalHeaders": { "__metadata": { "type": "Collection(SP.KeyValue)" }, "results": [ { "__metadata": { "type": 'SP.KeyValue' }, "Key": "content-type", "Value": 'text/html', "ValueType": "Edm.String" } ] } } }; return $.ajax({ contentType: "application/json", url: restUrl, type: "POST", data: JSON.stringify(mailObject), headers: restHeaders }); } </script> <input id="Button1" onclick="triggerEmail()" type="button" value="triggerEmail" />
Referenced link:
Best Regards,
Lee
Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com- Edited by Lee__liMicrosoft contingent staff Wednesday, August 23, 2017 6:29 AM
- Proposed as answer by Dennis Guo Thursday, August 31, 2017 9:51 AM
- Marked as answer by lmossolle Monday, November 25, 2019 6:06 PM
Wednesday, August 23, 2017 6:29 AM -
Hi ,
You can get the selected items using the below code
var ctx = SP.ClientContext.get_current();
var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);
for(var i=0; i < selectedItems.length;i++)
{
var itemId = selectedItems[i].id;
console.log(itemId);
}Please refer the following link for sending an Email
Please click the "Mark as Answer" button if this post solves your problem .
- Proposed as answer by Dennis Guo Thursday, August 31, 2017 9:51 AM
- Marked as answer by lmossolle Monday, November 25, 2019 6:06 PM
Tuesday, August 22, 2017 2:15 PM -
Do I just place that in the CEWP? Thanks
Lee Mossolle
- Marked as answer by lmossolle Monday, November 25, 2019 6:06 PM
Tuesday, August 22, 2017 2:19 PM -
Yes ... Please use sp.js references and jquery references
<script src="/_layouts/15/sp.js" type="text/javascript"></script>
- Marked as answer by lmossolle Monday, November 25, 2019 6:06 PM
Tuesday, August 22, 2017 2:36 PM
All replies
-
Hi ,
You can get the selected items using the below code
var ctx = SP.ClientContext.get_current();
var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);
for(var i=0; i < selectedItems.length;i++)
{
var itemId = selectedItems[i].id;
console.log(itemId);
}Please refer the following link for sending an Email
Please click the "Mark as Answer" button if this post solves your problem .
- Proposed as answer by Dennis Guo Thursday, August 31, 2017 9:51 AM
- Marked as answer by lmossolle Monday, November 25, 2019 6:06 PM
Tuesday, August 22, 2017 2:15 PM -
Do I just place that in the CEWP? Thanks
Lee Mossolle
- Marked as answer by lmossolle Monday, November 25, 2019 6:06 PM
Tuesday, August 22, 2017 2:19 PM -
Yes ... Please use sp.js references and jquery references
<script src="/_layouts/15/sp.js" type="text/javascript"></script>
- Marked as answer by lmossolle Monday, November 25, 2019 6:06 PM
Tuesday, August 22, 2017 2:36 PM -
I saved the js, added a CEWP, and pointed it to the JS still no luck
Lee Mossolle
Tuesday, August 22, 2017 3:02 PM -
Hi,
Raghava had pointed the solution, I just provide the tested sample code for your reference.
<script src="https://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script> <script type="text/javascript"> function triggerEmail() { var ctx = SP.ClientContext.get_current(); var listID = SP.ListOperation.Selection.getSelectedList(); var targetList = ctx.get_web().get_lists().getById(listID); var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx); for (var i = 0; i < selectedItems.length; i++) { var itemId = selectedItems[i].id; var item = targetList.getItemById(itemId); ctx.load(targetList, 'DefaultDisplayFormUrl'); ctx.load(item); ctx.executeQueryAsync( Function.createDelegate(this, function () { var itemTitle = item.get_item('Title'); var body = targetList.get_defaultDisplayFormUrl()+'?ID=' + itemId; sendMail('Administrator@contoso.com', ['user1@contoso.com'], 'CustomEmail' + itemTitle, 'item:' + body).done(function (response) { alert("Mail Sent."); }).fail(function (a,b,c) { alert(a.responseText); }); console.log(itemId); }), Function.createDelegate(this, function () { alert('error'); }) ); } } function sendMail(from,toList, subject, mailContent) { var restUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.Utilities.Utility.SendEmail", restHeaders = { "Accept": "application/json;odata=verbose", "X-RequestDigest": $("#__REQUESTDIGEST").val(), "Content-Type": "application/json;odata=verbose" }, mailObject = { 'properties': { '__metadata': { 'type': 'SP.Utilities.EmailProperties' }, 'From': from, 'To': { 'results': toList }, 'Subject': subject, 'Body': mailContent, "AdditionalHeaders": { "__metadata": { "type": "Collection(SP.KeyValue)" }, "results": [ { "__metadata": { "type": 'SP.KeyValue' }, "Key": "content-type", "Value": 'text/html', "ValueType": "Edm.String" } ] } } }; return $.ajax({ contentType: "application/json", url: restUrl, type: "POST", data: JSON.stringify(mailObject), headers: restHeaders }); } </script> <input id="Button1" onclick="triggerEmail()" type="button" value="triggerEmail" />
Referenced link:
Best Regards,
Lee
Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com- Edited by Lee__liMicrosoft contingent staff Wednesday, August 23, 2017 6:29 AM
- Proposed as answer by Dennis Guo Thursday, August 31, 2017 9:51 AM
- Marked as answer by lmossolle Monday, November 25, 2019 6:06 PM
Wednesday, August 23, 2017 6:29 AM