3

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?

3

You have to make a POST to _api/contextinfo to get a form digest. Scot Hillier has a library to help.

http://www.shillier.com/archive/2013/09/08/managing-sharepoint-2013-app-context.aspx

  • another approach: When using the JavaScript cross-domain library, SP.RequestExecutor handles getting and sending the form digest value for you, so you don't need to use $("#__REQUESTDIGEST").val(). source: msdn.microsoft.com/en-us/library/office/… – egidiocs Mar 9 '16 at 3:53
2

When using the JavaScript cross-domain library, SP.RequestExecutor handles getting and sending the form digest value for you, so you don't need to use $("#__REQUESTDIGEST").val()

executor = new SP.RequestExecutor(oWeb) ;
executor.executeAsync({your POST object})

source: https://msdn.microsoft.com/en-us/library/office/jj164022.aspx#WritingData

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.