How to Send an Email in a SharePoint Hosted app?
We can send email to the Users / Group in SharePoint Hosted app by using workflow Email Activity.
In SharePoint 2013 Online there aren't any API's available to send email, so to send email in Office 365 online, we'll have to do the following,
While Creating SharePoint Hosted app In Visual Studio, Create a site workflow, after that from the toolbox add the "Email" Activity to workflow,
This activity requires, "TO", "Subject" and "Body" arguments.
Now we'll create the three arguments of the workflow, we'll pass these arguments when we'll start the workflow.
Now you'll need to provide activity arguments, Body and Subject arguments are simple you can just pass the argBody and argSubj workflow arguments as is,
but for TO field you'll have to covert the string argTOEmail into Collection,
We'll create a new Collection object of type string.
add this to create the Email collection,
new System.Collections.ObjectModel.Collection<string>() { argTOEmail }
our workflow is now complete,
We'll start our email workflow using JavaScript Object Model. Include the following code snippet in your app.js file,
var context = null;
var web;
var wfManager;
var subscription;
var params;
var _wss;
var argSubj;
var argBody;
var argTOEmail;
function send(Subject, Body, ToEmail) {
context = SP.ClientContext.get_current();
web = context.get_web();
argSubj = Subject;
argBody = Body;
argTOEmail = ToEmail;
wfManager = new SP.WorkflowServices.WorkflowServicesManager.newObject(context, web);
context.load(wfManager);
context.executeQueryAsync(initWorkflowSubScriptionService);
}
The above code will load the Workflowmanager and on-success will call initWorkflowSubScriptionService function.
initWorkflowSubScriptionService will start the workflow, and hence will send the email,
function initWorkflowSubScriptionService() {
_wss = wfManager.getWorkflowSubscriptionService();
context.load(_wss);
context.executeQueryAsync(
function (sender, args) {
subscription = _wss.getSubscription("f248e55e-495c-4c9b-b4de-618033d6be3b");
params = new Object();
params["argSubj"] = argSubj;
params["argBody"] = argBody;
params["argTOEmail"] = argTOEmail;
context.executeQueryAsync(
function (sender, args) {
wfManager.getWorkflowInstanceService().startWorkflow(subscription, params);
context.executeQueryAsync(
function (sender, args) {
},
function (sender, args) {
console.log("Something went wrong in starting workflow: " + args.get_message() + '\n' + args.get_stackTrace());
}
)
}
)
},
function (sender, args) {
console.log("Something went wrong in starting workflow: " + args.get_message() + '\n' + args.get_stackTrace());
}
);
}
So now you'll just need to call the function send like this
send ("This is Subject","This is body","test@test.onmicrosoft.com");
There are however some restrictions,
You can send email only to the Current Domain users.
and using the above approach you can only send email to only one recipient,
I'll write another blog post on how to send Email to Multiple recipient in SharePoint Hosted app using workflow and Javascript Object Model