Madhanmohan Devarajan

Madhanmohan Devarajan

  • 231
  • 8.2k
  • 1.7m

How to attach pdf file to SharePoint REST API Send Email?

Dec 1 2016 10:55 AM
Trying to attach a pdf file in an email, currently using SharePoint REST API to send email from AngularJS service module.
Have shared the code snippet below.
Please help me if you have some idea to do it. Here i don't have any issues with the code.
 
Flow of the application: View (HTML form button click) -> Controller -> Service -> BaseSvc
 
 BaseSvc:
  
(function () {
angular.module("peopleApp")
.factory("baseSvc", ["$http", "$q", function ($http, $q) {
 var baseUrl = _spPageContextInfo.webAbsoluteUrl;
  
var sendEmailRequest = function (emailFrom,emailBody,emailTo,emailSub)
{
var deferred = $q.defer();
//Get the relative url of the site
var urlTemplate = baseUrl + "/_api/SP.Utilities.Utility.SendEmail";
$http({
contentType: 'application/json',
url: urlTemplate,
type: "POST",
data: JSON.stringify({
'properties': {
'__metadata': {
'type': 'SP.Utilities.EmailProperties'
},
'From': emailFrom,
'To': {
'results': [emailTo]
},
'Body': emailBody,
'Subject': emailSub
}
}),
headers: {
"Accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest":document.getElementById("__REQUESTDIGEST").value
},
}).success(function(result) {
deferred.resolve(result);
}).error(function(result, status) {
deferred.reject({
error: result,
status: status
})
});
return deferred.promise;
};
return {
sendEmailRequest: sendEmailRequest
};
}]);
})();
 
 Service:
"use strict";
(function(){
angular.module("peopleApp")
.factory("peopleService",["baseSvc",function(baseService){
var listEndPoint = '/_api/web/lists';
 
var sendEmail = function(){
var eFrom = '[email protected]',
eBody = 'Dear member you got mail from SharePoint',
eSub = 'Email from REST API';
// Call sendEmail function
return baseService.sendEmailRequest(eFrom, eBody, eTo, eSub);
};
 
return{
sendEmail:sendEmail
};
}]);
})();
 
Controller:
 
"use strict";
(function () {
angular.module("peopleApp")
.controller("allPeopleCtrl", ["$scope", "peopleService","$location",
 
$scope.sendEmail=function (){
//var doc = pdfMake.createPdf(docDefinition);
peopleService.sendEmail()
.then(function (response) {
console.log(response);
$location.path("/");
});
};
 
$scope.downloadPdf = function () {
var employees = $scope.people;
var empList = [];
angular.forEach(employees, function(value, key) {
empList.push({Firstname: value.FirstName, Lastname: value.LastName, Address: value.Address});
});
var items = empList.map(function(item) {
return [item.Firstname, item.Lastname, item.Address];
});
var docDefinition = {
pageOrientation: 'landscape',
content: [
{ text: 'Employees List'},
{
style: 'styleTable',
table:
{
body:
[
[{ text: 'Firstname', style: 'header' },
{ text: 'Lastname' , style: 'header' },
{ text: 'Address' , style: 'header' },
],
].concat(items)
}
}
],
styles: {
header: {
bold: true,
color: '#000',
fontSize: 11
},
styleTable: {
color: '#666',
fontSize: 10
}
}
};
pdfMake.createPdf(docDefinition).download("EmployeesNew.pdf");
};
 
};
}]);
})();
 
 

Answers (2)