Sending an Email in SharePoint 2013 via Javascript (REST API)

I was looking for a way to send an email in SharePoint that didn’t require a compiled solution, since that usually involved scheduled maintenance windows.  I stumbled upon “SP.Utilities.Utility.SendEmail” in my searches.  This is a super simple way to get an email sent, with minimal hassle.

There really is only one catch here. The email recipient(s) must be users that have visited the site.  (ie. company email accounts).  Random email accounts that have not been authenticated to SharePoint will throw an error.

I will also note, that if you get a 403 Forbidden error, the problem could be one of two things.

  1. You should first check to make sure you are getting the digest. A simple “alert” should let you know.  If you are not getting the digest (ie. maybe your form isn’t in a SP masterpage), try using Scot Hillier’s “Wingtip” helper to grab it: http://www.shillier.com/archive/2013/09/08/managing-sharepoint-2013-app-context.aspx
  2. If you are getting the digest, it could be the same issue I ran into. I had an older version of jQuery (1.4.2).  Upgrading to a newer version (1.11) solved the 403 issue for me.

The HTML can be added to a simple Content Editor Web Part, or elsewhere…

​​Full Name: ​<br/>
<input type="text" name="fname" id="fname" placeholder="Your Full Name"/><br/> 
Your Email: <br/>
<input type="text" name="femail" id="femail" placeholder="Your Email Address"/><br/>
Your Message: <br/>
<textarea rows="4" cols="50" id="fmessage"></textarea><br/><br/>
<button type="button" id="fbutton">Send Message</button> 
<script type="text/javascript" src="jquery.js"></script>​​​​​​​​​​​​ 
<script type="text/javascript" src="emailform.js"></script> ​​​​​​​​​​​​​​

emailform.js code:

$(document).ready(function() {
    $("#fbutton").click(function(){
       var siteurl = _spPageContextInfo.webServerRelativeUrl;
       var name = $("#fname").val();
       var from = $("#femail").val();
       var msg = 'From: ' + name + '<br/><br/>' + 'Email: ' + from + '<br/><br/><br/>' + $("#fmessage").val();

       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': ['jdoe@company.com'] },
              'Body': msg,
              'Subject':'New Message From SharePointWebsite'
             }
           }),
         headers: {
            "Accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()                 
         },
         success: function (data) {
           alert('Your message has been sent');
           $("#fname").val('');
           $("#femail").val('');
           $("#fmessage").val('');
         },
         error: function (err) {
            alert(JSON.stringify(err));
         }
      });
   });          
});

Errors When Adding SP2013 Navigation in SP2010 UI Mode

There is an rather obscure error that can occur when working with “Audiences” and groups in SharePoint 2010 mode within SharePoint 2013. The problem can surface when trying to create list items based around audiences. Items can be created just fine, but when trying to add an audience trimming to the item, SharePoint can throw an error. This is related to how the form within IE is storing the HTML for the audience.

To get around this, you will need to change your Compatibility View setting within IE.

See example:

Add A Link:

click-add-link

Create a link and give it an audience trimming:

adding-a-sharepoint-link

SharePoint Throws An Error:

error-adding-navigation

Navigate to Compatibility Setttings:

go-to-compatibility

Change Compatibility Settings to use Compatibility View:

change-compatibility-for-sh

Navigational items should now save successfully.

Log In as different user (SP2013)

SharePoint 2013 took away the ability to log in as a different user via the drop down menu. Should you need to log in as a different account, try adding the following to the page you are on:

/_layouts/closeConnection.aspx?loginasanotheruser=true