Wednesday, August 19, 2015

Sending E-mail in Sharepoint using CSOM(Client Side Object Model)

E-mail in Sharepoint using CSOM


Using SharePoint Client Object Model (CSOM) has been a most famous method for a sending email. You can use SharePoint Utility class to send a Email. But you can not send to external users. If you are sending to external users they should be added to your mail exchange but it will take some time to reflect the change.


function sendEmail(from, to, body, subject) 

var siteurl = _spPageContextInfo.webServerRelativeUrl;

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': [to] ,
'Body': body,
'Subject': subject


),
headers:
"Accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
,
success: function (data)
alert("Eposten ble sendt");
,
error: function (err)
alert(err.responseText);
debugger;

);

The other way to send an E-mail in sharepoint is using javascript model (JSOM)


Using SharePoint JavaScript Model (JSOM) is very similar to the CSOM but it will use only JavaScript for sending emails.


var mail = 
properties:
__metadata: 'type': 'SP.Utilities.EmailProperties' ,
From: 'from@mail.com',
To: 'results': ['one@mail.com','two@mail.com'] ,
Body: 'some body',
Subject: 'subject'

;

var getAppWebUrlUrl = decodeURIComponent(utils.getQueryStringParameter("SPAppWebUrl").replace("#", ""));
var urlTemplate = getAppWebUrlUrl + "/_api/SP.Utilities.Utility.SendEmail";

$.ajax(
contentType: 'application/json',
url: urlTemplate,
type: "POST",
data: JSON.stringify(mail),
headers:
"Accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
,
success: function (data)
// code
,
error: function (err)
// code

);


Sending E-mail in Sharepoint using CSOM(Client Side Object Model)

No comments:

Post a Comment