I needed to change the delete button on the ribbon on some specific App parts on SharePoint 2013, so looking around I found a post that shows how to trigger an event with each ribbon change by Dennis George which was of great help because it did exactly what I needed, to capture when a certain tab was selected. The issue? It doesn’t work on SharePoint 2013 because the CUI.Ribbon.prototype.$L is always undefined, so I started debugging the whole ribbon and found that to make the code work in SharePoint 2013 I just had to change $L with $1q_0 and that was it. So here’s the code to trigger an event each time there is a change on the SharePoint 2013 ribbon. Use it at will:
// Fires 'ribbontabselected' every time the ribbon selection changes ExecuteOrDelayUntilScriptLoaded(function () { debugger; CUI.Ribbon.prototype.$1q_0_old = CUI.Ribbon.prototype.$1q_0; CUI.Ribbon.prototype.$1q_0 = function () { this.$1q_0_old(); $(document).trigger('ribbontabselected', [this.get_selectedTabCommand()]); }; }, 'cui.js'); // Fires 'ribbontabselected' after the ribbon has been initialized after load ExecuteOrDelayUntilScriptLoaded(function () { debugger; var pm = SP.Ribbon.PageManager.get_instance(); pm.add_ribbonInited(function () { $(document).trigger('ribbontabselected', [SP.Ribbon.PageManager.get_instance().get_ribbon().get_selectedTabCommand()]); }); }, 'sp.ribbon.js'); // Example code for binding to the event $(document).on('ribbontabselected', function (e, selectedTabCommand) { debugger; if (selectedTabCommand != "ReadTab") { alert(selectedTabCommand); } });
good find, very nice! thanks for the pingback!
LikeLike
Thanks to for that great help you’ve given me 😉
LikeLike
You are the best!. thanks!!! 🙂 🙂 🙂
LikeLike
Glad to help!
LikeLike
Thank you for the code snippet. It works like a charm.
LikeLike
Glad to help!
LikeLike
You saved my day !
LikeLike
Glad to help!
LikeLike
but i changed in the last block
i used $(document).bind instead of $(document).on
LikeLike
You could use both of them, though jQuery is recommending using .on since version 1.7. Regards.
LikeLike
how does this work?I added it through CEWP on allitems.aspx and entire ribbon was hidden. I add it though JSLink , ribbon was visible but all ribbon buttons were disabled.
LikeLike
You should add it inside a Script Editor Web Part or putting it on a separate file and reference it on the master page or on the layout of your need. Let me know if you have any issues.
LikeLike
Dear Santiago I found that on IE 10 (not tested with other version thought) on our Sharepoint 2013 (on which backend indeed I don’t have any control as I’m working with CSR/JS Link) the first `ExecuteOrDelayUntilScriptLoaded` is called twice so that the `$1q_0_old` is override in the second call with an infinite recursion.
I added a simple test to avoid the issue:
“`
if (typeof CUI.Ribbon.prototype.$1q_0_old == ‘undefined’) {
CUI.Ribbon.prototype.$1q_0_old = CUI.Ribbon.prototype.$1q_0;
}
“`
LikeLike
//Fires ‘ribbontabselected’ every time the ribbon selection changes
ExecuteOrDelayUntilScriptLoaded(function () {
CUI.Ribbon.prototype.$S_0_old = CUI.Ribbon.prototype.$S_0;
CUI.Ribbon.prototype.$S_0 = function () {
this.$S_0_old();
$(document).trigger(‘ribbontabselected’, [this.get_selectedTabCommand()]);
};
}, ‘cui.js’);
For SP2016
LikeLike
Hi
Using “private” functions may cause problems ie. it may change name between CU versions, so instead of using:
ExecuteOrDelayUntilScriptLoaded(function () {
CUI.Ribbon.prototype.$1q_0_old = CUI.Ribbon.prototype.$1q_0;
CUI.Ribbon.prototype.$1q_0 = function () {
this.$1q_0_old();
$(document).trigger(‘ribbontabselected’, [this.get_selectedTabCommand()]);
};
}, ‘cui.js’);
use:
ExecuteOrDelayUntilScriptLoaded(function () {
CUI.Tab.prototype.set_selected_old = CUI.Tab.prototype.set_selected;
CUI.Tab.prototype.set_selected = function (a) {
this.set_selected_old(a);
$(document).trigger(‘ribbontabselected’, [this.get_ribbon().get_selectedTabCommand()]);
};
}, ‘cui.js’);
LikeLike