I'm trying to post a REST query to SP.Utilities.Utility.Sendmail so I can send a mail from my sharepoint hosted app. For this I have the following function:
function sendEmail(from, to, body, subject, success, fail) {
appweburl = decodeURIComponent(getQueryStringParameter('SPAppWebUrl'));
hostweburl = decodeURIComponent(getQueryStringParameter('SPHostUrl'));
var urlTemplate = appweburl + "/_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) {
success();
},
error: function (err) {
fail(err);
}
});
This code has worked for me in the past, so I know it's correct. For this project however, I'm trying to send a mail from a modal dialog inside my appweb. This modal dialog page has no masterpage.
Because this page has no masterpage, it's missing the $("#__REQUESTDIGEST").val() and I get a 403 forbidden error. I tried to fix this by passing the REQUESTDIGEST of my parent page, and use this one, however, this gives me a 400 bad request error.
Adding <SharePoint:FormDigest ID="FormDigest1" runat="server"></SharePoint:FormDigest>
doesn't work either since IIS doesn't process it obviously.
Does anybody have any idea what the correct approach is to getting a REQUESTDIGEST in a page without a masterpage?