Sep 19, 2017

Delete SharePoint calendar event with JavaScript


Technorati Tags: ,
If you ever need to override ribbon actions or to implement custom calendar in SharePoint working with the out-of-the-box calendar list, you will end up implementing create, update and delete operations for your events.
The possibility of having re-occurring events makes the implementation of delete operation quite complex, because you would usually aim to delete single occurrence rather the whole series of given event.
The best approach would be to find out how to invoke the ootb function of the operation and develop your own custom code around it.
The server side logic implementation for the calendar is residing in Microsoft.SharePoint.ApplicationPages.Calendar.dll , and the client one could be found in SharePoint hive folder TEMPLATE\LAYOUTS - SP.UI.ApplicationPages.Calendar.js (respectively SP.UI.ApplicationPages.Calendar.debug.js).
If you search in this JavaScript file by "delete", you will find right away next function:
deleteItem: function SP_UI_ApplicationPages_CalendarContainer$deleteItem$in(itemId) {

        var $v_0 = window['ctx' + this.$U_1.ctxId];

        var $v_1;

        var $v_2 = $v_0['RecycleBinEnabled'];

 

        if (itemId.indexOf('.0.') !== -1 || itemId.indexOf('.1.') !== -1) {

            $v_1 = SP.Res.calendarDeleteConfirm;

        }

        else if (!SP.UI.ApplicationPages.SU.$1($v_2) && $v_2 === '1') {

            $v_1 = window.Strings.STS.L_STSRecycleConfirm_Text;

        }

        else {

            $v_1 = window.Strings.STS.L_STSDelConfirm_Text;

        }

        if (!confirm($v_1)) {

            return;

        }

        this.$c_1.$9M_0(itemId);

    }
It looks to be exactly the function that is called when we press delete:
deleteEvent_UI
The problem is that the delete action and invocation of the server side happens through calling this.$c_1.$9M_0(itemId);
There are two challenges here:
  • What is the value of the argument itemId that we must provide to make a valid invocation
  • Can we directly call $c_1.$9M_0 function
The latter is not a good approach considering Microsoft may change it in future upgrades/releases.
So, the only feasible solution would be how to call the deleteItem function and to pass valid argument value to it, so it can carry out the action for us.
If you scroll up in the file in the SP.UI.ApplicationPages.Calendar.debug.js, you will find out that the deleteItem function is defined for SP.UI.ApplicationPages.CalendarContainer object.
Calling SP.UI.ApplicationPages.CalendarInstanceRepository.firstInstance() is giving us the reference we need.
The value we need to pass is the value of the ID from the url.
It could be just a figure: ID=1, or combination of ID and time for repeating events: ID=2.0.2017-09-03T15:00:00Z
You can easily parse the URL and get the values from query string:
var readQueryString = function (src, key) {

    key = key.replace(/[*+?^$.\[\]{}()|\\\/]/g, "\\$&"); // escape RegEx meta chars

    var match = src.match(new RegExp("[?&]" + key + "=([^&]+)(&|$)"));

    return match && decodeURIComponent(match[1].replace(/\+/g, " "));

}

var id = readQueryString(this.location.href, "ID");
SP.UI.ApplicationPages.CalendarInstanceRepository.firstInstance().deleteItem(id);
You can give it a try in the console:
deleteEvent_JS
The solution about has been validated for on premises implementation, but it should be also applicable to o365 environment.
Read full article!