Sending email through AJAX calculated column

580 views Asked by At

I'm trying to send an automatic email receipt for items that have been created on a sharepoint list.

Conditions

  1. I cannot use workflows - they are disabled
  2. I cannot use webparts - they are disabled
  3. I cannot use sharepoint designer etc etc etc - they are all disabled
  4. The function needs to be OOTB

The only option I have is using javascript in calculated columns. I am aware of the use of the HTML:mailto tag - but this opens Microsoft outlook, and is not automatic.

Came across this link:

http://geekswithblogs.net/ThorvaldBoe/archive/2014/07/03/sending-email-with-sharepoint-and-jquery.aspx

So, here is the calculated column attempt:

="<button onclick=""{function SendEMail(from, to, body, subject){"
&"var siteurl = _spPageContextInfo.webServerRelativeUrl;"
&"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('Eposten ble sendt');"
&"},error: function (err) {"
&" alert(err.responseText);"
&" debugger;}});}"
&"SendEMail('[email protected]','[email protected]','Test1','Test2');}"">"&"Send</button>"

When running the code, the Console shows the following error: '$' is undefined

Any suggestions on how to overcome this?

Thanks

1

There are 1 answers

0
Danny '365CSI' Engelman On

You keep raising the bar, Steve!

You're really making it difficult for yourself this way

I suggest the next learning steps first:

  • Learn all about Chrome Snippets
    this will help you develop/execute code directly on the View page without the need for stuffing it in a Calculated Column or any script in SharePoint

  • Learn jQuery (by using Snippets)
    check out jQuerify it will inject jQuery where it is not available

  • [optional] Learn Tampermonkey
    this will help in immediatly executing your scripts on a SharePoint page (without the need for including the script in SharePoint

  • Learn JSOM Ajax calls and REST Ajax calls (REST is halfbaked implemented in 2010)
    again; do them in Snippets first

When that all works you're halfway done and you can wrap it all in the Calculated Column (again, this is all undocumented hacking away at Microsoft technology, if they decide to make changes in an Update you're cooked... e.g they disabled the use of the SCRIPT tag in summer 2013; that's why you now have to use the blank IMG onload trick

Note: I have updated my CalcMaster BookMarklet on GitHub. Once you have your Snippet working you can paste it in an existing Calculated Column Formula and wrap it in IMG onload and &".." notation with one click

If you get that sending email working let us know; I have never done it... I stay away from projects where SharePoint Designer can not be used.

Once you have mastered all the above you will have learned so much Front-End development I suggest you go look for another job.