AnsweredAssumed Answered

How do I call SharePoint 2013 Rest APIs from Nintex Form

Question asked by johncoltrane on Jan 29, 2016
Latest reply on Feb 29, 2016 by patrickabel

I attempting to call a Rest API from a Nintex form. Here is the Rest procedure I'm attempting to call:

 

function sendEmail(from, to, body, subject) {

//var siteurl = _spPageContextInfo.webServerRelativeUrl;

//siteurl = "absoluteURL";

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("email sent");

   },

 

 

   error: function (err) {

       alert(err.responseText);

       debugger;

   }

});

}

 

The issue is exactly how to call the procedure. I have attempted the following:

  1. NWF$(document).ready(function() {

     SP.SOD.executeOrDelayUntilScriptLoaded(processSendEmails, 'sp.js');

     });

The procedure is not called.

     2. 

var pollSP;

NWF.FormFiller.Events.RegisterAfterReady(function () {

    pollSP = setInterval(checkSPLoad, 500);

});

function checkSPLoad() {

    if (clientContext) {

        window.clearInterval(pollSP);

        onSPLoad();

    }

}

function onSPLoad() {

     //Our code must come here

alert("starting");

var context = new SP.ClientContext.get_current();

var web = context.get_web();

context.load(web);  

context.executeQueryAsync(function(){

sendEmail("email1","email2","body text","subject test",web.get_url());

  },

  function(sender,args){

alert("error function");

     alert(args.get_message());

  }

); 

}

With this method the procedure is called, but an error -

Unexpected response data from server

3. Which me to attempt the factory approach, since this generally suggests a cross-domain scripting issue:

var appweburl = "web address";

var clientContext = new SP.ClientContext(appweburl);

var factory = new SP.ProxyWebRequestExecutorFactory(appweburl);

clientContext.set_webRequestExecutorFactory(factory);

var appWeb = clientContext.get_web();

Once again the procedure was not called. Can someone please suggest the correct manner to access the Rest API?

Thanks in advance.

Outcomes