REST API is used for remotely interacting with SharePoint sites. Now, you can interact directly with SharePoint objects by using any technology that supports standard REST capabilities.
REST API uses HTTP protocol, OData protocol, Client.SVC Web Service, and JSON (JavaScript Object Notation) to communicate between apps and SharePoint.
The below example is used to send email notification to users after submission of the list item in the List.
Find the below steps to send email notification.
- Create project, add the list to Site Collection, and code for submission.
- Send e-mail notification to users, using REST API for Button click.
For the first step, please find the below article about adding list, creation of project, and code for submission.
For the second step -
- Write the method for sending email notification in App.js file.
- Call the method under Submit button after inserting data to list.
- Provide the Alert for a successful message.
Write the below method in App.js file for sending e-mail notification through REST API.
- function processSendEmail() {
-
- if ($("#empEmailId").val() != null) {
- var userEmaiId = $("#empEmailId").val();
- }
- else {
-
- alert("EmailId Should not be empty");
- }
-
-
-
- var From = '';
- var To = userEmaiId;
- var Body = 'You have been successfully submitted the data to List';
- var SubjectOfEmail = 'Email Notification Using REST API';
-
-
- sendEmailNotification(From, To, Body, SubjectOfEmail);
- }
-
- function sendEmailNotification(From, To, Body, SubjectOfEmail) {
-
- hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
-
- var constructedURL = appWebUrl + "/_api/SP.Utilities.Utility.SendEmail";
- $.ajax({
- contentType: 'application/json',
- url: constructedURL,
- type: "POST",
- data: JSON.stringify({
- 'properties': {
- '__metadata': { 'type': 'SP.Utilities.EmailProperties' },
- 'From': From,
- 'To': { 'results': [To] },
- 'Body': Body,
- 'Subject': SubjectOfEmail
- }
- }
- ),
- headers: {
- "Accept": "application/json;odata=verbose",
- "content-type": "application/json;odata=verbose",
- "X-RequestDigest": $("#__REQUESTDIGEST").val()
- },
- success: function (data) {
- alert('Email Sent Successfully');
-
-
- },
- error: function (err) {
- alert('Error in sending Email: ' + JSON.stringify(err));
-
-
- }
- });
- }
After completion of code, deploy the solution, and trust it for Site Collection.
The e-mail id which you are going to enter in textbox, should be of the users that are from the group in SharePoint.
I am attaching the source code for this. Please try and let me know if you face any issues.