Introduction
The REST API in SharePoint 2013 provides developers with a simple standardised method of accessing information contained within SharePoint. It can be used from any technology that is capable of sending standard http requests and is particularly useful for developers who are not familiar with the Client Side Object Model.
For a full list of advantages and disadvantages of the REST API, and for a comparison with other API’s click here.
You should be able to send an email from a hosted app, if you have the mail server set up correctly in SharePoint Foundation. Check out this link on how to Configure outgoing email for a SharePoint 2013 farm.
REST URI Operators
Once you have the correct base URI the next step is to determine the correct method for sending mail. To send a mail, SharePoint REST API exposes the following method 'SendEmail' as an endpoint which is in the class SP.Utilities.Utility.
The example below is to send a mail using ajax call.
var urlTemplate =_spPageContextInfo.webAbsoluteUrl + "/_api/SP.Utilities.Utility.SendEmail";
$.ajax({
contentType: 'application/json',
url: urlTemplate,
type: "POST",
data: JSON.stringify({
'properties': {
'__metadata': { 'type': 'SP.Utilities.EmailProperties' },
'From': 'Hemanth.Anumolu@somewhere.com',
'To': { 'results': ['Test@somewhere.com'] },
'Body': '<h1>Hello!!</h1><p>This is mail was sent from a client side function</p>',
'Subject':'Send eMail'
}
}
),
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(JSON.stringify(err));
}
});
OR
The REST API in SharePoint 2013 provides developers with a simple standardised method of accessing information contained within SharePoint. It can be used from any technology that is capable of sending standard http requests and is particularly useful for developers who are not familiar with the Client Side Object Model.
For a full list of advantages and disadvantages of the REST API, and for a comparison with other API’s click here.
You should be able to send an email from a hosted app, if you have the mail server set up correctly in SharePoint Foundation. Check out this link on how to Configure outgoing email for a SharePoint 2013 farm.
REST URI Operators
Once you have the correct base URI the next step is to determine the correct method for sending mail. To send a mail, SharePoint REST API exposes the following method 'SendEmail' as an endpoint which is in the class SP.Utilities.Utility.
The example below is to send a mail using ajax call.
var urlTemplate =_spPageContextInfo.webAbsoluteUrl + "/_api/SP.Utilities.Utility.SendEmail";
$.ajax({
contentType: 'application/json',
url: urlTemplate,
type: "POST",
data: JSON.stringify({
'properties': {
'__metadata': { 'type': 'SP.Utilities.EmailProperties' },
'From': 'Hemanth.Anumolu@somewhere.com',
'To': { 'results': ['Test@somewhere.com'] },
'Body': '<h1>Hello!!</h1><p>This is mail was sent from a client side function</p>',
'Subject':'Send eMail'
}
}
),
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(JSON.stringify(err));
}
});
OR
<input type="text" name="fname" id="fname" placeholder="Your Full Name"/><br/> Your Email: <br/> <input type="text" name="femail" id="femail" placeholder="Your Email Address"/><br/> Your Message: <br/> <textarea rows="4" cols="50" id="fmessage"></textarea><br/><br/> <button type="button" id="fbutton">Send Message</button> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="emailform.js"></script>
emailform.js code:
$(document).ready(function() {
$("#fbutton").click(function(){
var siteurl = _spPageContextInfo.webServerRelativeUrl;
var name = $("#fname").val();
var from = $("#femail").val();
var msg = 'From: ' + name + '<br/><br/>' + 'Email: ' + from + '<br/><br/><br/>' + $("#fmessage").val();
var urlTemplate = siteurl + "_api/SP.Utilities.Utility.SendEmail";
$.ajax({
contentType: 'application/json',
url: urlTemplate,
type: "POST",
data: JSON.stringify({
'properties': {
'__metadata': { 'type': 'SP.Utilities.EmailProperties' },
'From': from,
'To': { 'results': ['jdoe@company.com'] },
'Body': msg,
'Subject':'New Message From SharePointWebsite'
}
}),
headers: {
"Accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
alert('Your message has been sent');
$("#fname").val('');
$("#femail").val('');
$("#fmessage").val('');
},
error: function (err) {
alert(JSON.stringify(err));
}
});
});
});
OR
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
Full Name: <br/>
<input type="text" name="fname" id="fname" placeholder="Your Full Name"/><br/>
Your Email: <br/>
<input type="text" name="femail" id="femail" placeholder="Your Email Address"/><br/>
Your Message: <br/>
<textarea rows="4" cols="50" id="fmessage"></textarea><br/><br/>
<button type="button" id="fbutton">Send Message</button>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="emailform.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#fbutton").click(function(){
var siteurl = _spPageContextInfo.webServerRelativeUrl;
var name = $("#fname").val();
var from = $("#femail").val();
var msg = 'From: ' + name + '<br/><br/>' + 'Email: ' + from + '<br/><br/><br/>' + $("#fmessage").val();
var urlTemplate = siteurl + "_api/SP.Utilities.Utility.SendEmail";
$.ajax({
contentType: 'application/json',
url: urlTemplate,
type: "POST",
data: JSON.stringify({
'properties': {
'__metadata': { 'type': 'SP.Utilities.EmailProperties' },
'From': from,
'To': { 'results': ['XYZ@gmail.com'] },
'Body': msg,
'Subject':'New Message From SharePointWebsite'
}
}),
headers: {
"Accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data) {
alert('Your message has been sent');
$("#fname").val('');
$("#femail").val('');
$("#fmessage").val('');
},
error: function (err) {
alert(JSON.stringify(err));
}
});
});
});
</script>
No comments:
Post a Comment