Send an invitation to the creator of an event using Calendar Email Extension

A few of your customers recently asked if it’s possible to send an invitation to the creator of an event automatically using the Calendar E-mail Extension. The answer is yes, but the creator has to be added to the ‘Attendees’ column.

Since it’s not possible to set a default value for a user column (people picker), we have to do it using JavaScript.

Here is the entire script:

ExecuteOrDelayUntilScriptLoaded(init_setDefaultUser,'sp.js');
var currentUser;
var currentUserLoginName;
var currentUserTitle;
function init_setDefaultUser(){
    //get the current user
    var clientContext = new SP.ClientContext.get_current();
    var oWeb = clientContext.get_web();
    currentUser = oWeb.get_currentUser();
    clientContext.load(currentUser);
    clientContext.executeQueryAsync(onQuerySucceededCurrentUser, onQueryFailedCurrentUser);
}
function onQueryFailedCurrentUser(sender, args) { }
function onQuerySucceededCurrentUser() {
    //get login name and display name of the current user
    currentUserLoginName = currentUser.get_loginName(); 
    currentUserTitle = currentUser.get_title();
    //call fillDefaultUser 
    fillDefaultUser();
}
function fillDefaultUser()
{
    //fill the second people picker
    fillPeoplePickerWithCurrentUser(2);
}
function fillPeoplePickerWithCurrentUser(pickerNo)
{
    if(currentUser != null)
    {
      //get the people picker with the given number
      var pp = getPickerImputElement(pickerNo);
      if(pp != null)
      {
          //build entities xml
          var key = currentUserLoginName;
          var dispval = currentUserTitle;
          var xml = '<Entities Append="False" Error="" Separator=";" MaxHeight="3">';
          xml = xml + '<Entity Key="' + key + '" DisplayText="' + dispval + '" IsResolved="True" Description="' + key + '"><MultipleMatches /></Entity>'
          xml = xml + '</Entities>';
          //call EntityEditorCallback
          EntityEditorCallback(xml, pp.id, true);
      }
    }
}
function getPickerImputElement(pickerNo)
{
   var result = '';
   //get all people picker controls
   var pp = document.getElementsByClassName('ms-usereditor');
   var j = 0;
   for(var i=0; i < pp.length; i++)
   {
      //only if control id ends with _UserField
      if(pp[i].id.indexOf('_UserField', pp[i].id.length - '_UserField'.length) !== -1)
      {
         j++;
         if(j == pickerNo)
         {
            //this is the picker we are looking for
            result = pp[i];
            break;
         }
      }
   }
   return result;
}
ExecuteOrDelayUntilScriptLoaded(init_setDefaultUser,'sp.js');
var currentUser;
var currentUserLoginName;
var currentUserTitle;
function init_setDefaultUser(){
    //get the current user
    var clientContext = new SP.ClientContext.get_current();
    var oWeb = clientContext.get_web();
    currentUser = oWeb.get_currentUser();
    clientContext.load(currentUser);
    clientContext.executeQueryAsync(onQuerySucceededCurrentUser, onQueryFailedCurrentUser);
}
function onQueryFailedCurrentUser(sender, args) { }
function onQuerySucceededCurrentUser() {
    //get login name and display name of the current user
    currentUserLoginName = currentUser.get_loginName(); 
    currentUserTitle = currentUser.get_title();
    //call fillDefaultUser 
    fillDefaultUser();
}
function fillDefaultUser()
{
    //fill the second people picker
    fillPeoplePickerWithCurrentUser(2);
}
function fillPeoplePickerWithCurrentUser(pickerNo)
{
    if(currentUser != null)
    {
      //get the people picker with the given number
      var pp = getPickerImputElement(pickerNo);
      if(pp != null)
      {
          //build entities xml
          var key = currentUserLoginName;
          var dispval = currentUserTitle;
          var xml = '<Entities Append="False" Error="" Separator=";" MaxHeight="3">';
          xml = xml + '<Entity Key="' + key + '" DisplayText="' + dispval + '" IsResolved="True" Description="' + key + '"><MultipleMatches /></Entity>'
          xml = xml + '</Entities>';
          //call EntityEditorCallback
          EntityEditorCallback(xml, pp.id, true);
      }
    }
}
function getPickerImputElement(pickerNo)
{
   var result = '';
   //get all people picker controls
   var pp = document.getElementsByClassName('ms-usereditor');
   var j = 0;
   for(var i=0; i < pp.length; i++)
   {
      //only if control id ends with _UserField
      if(pp[i].id.indexOf('_UserField', pp[i].id.length - '_UserField'.length) !== -1)
      {
         j++;
         if(j == pickerNo)
         {
            //this is the picker we are looking for
            result = pp[i];
            break;
         }
      }
   }
   return result;
}

The easiest way is to add the script to the new form of the SharePoint calendar using a Content Editor or Script Editor Web Part.

You can of course also use this script to set the default user for other columns, just change the number in the fillDefaultUser method in the script.