Asynchronous Conditional Enabling of Buttons
How to set Enabled state of button in asynch client-side call
|
Bjørn Furuknap
Guest
|
I'd like to use a client-side script to control enabling of buttons in the ribbon. I've successfully used the OnActivate event to accomplish this for synchronous conditions (number of items selected, for example)
I need to accomplish the same task using asynchronous calls to SharePoint in order to control enabling of buttons based on properties of selected items. However, when I set the Enabled property to true in the success method delegates, it seems that this property has no effect. If I set it prior to the asynchronous call (standard executeQueryAsync), everything works, so it's only when setting this as part of the asynch method.
// This line works if uncommented
// button.Enabled = false;
ctx.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
function onQuerySucceeded() {
if (some_Condition) {
// This line does not work
button.Enabled = false;
}
}
Any ideas how I can accomplish this on the client side? Server-side code is not an option, I'm afraid :-(
.b |
|
Posted 16 Feb, 2013 14:39:35
|
|
Top
|
|
Sergey Grischenko
Add-in Express team
Posts: 7233
Joined: 2004-07-05
|
Hi Bjørn,
The fact is that the 'Enabled' property doesn't refresh the ribbon.
You need to call the 'get_<ribbon feature name>().RefreshRibbon()' method after you changed the 'Enabled' property of the control (e.g. get_MyRibbon().RefreshRibbon()).
Or just use the 'control.setEnabled(<true or false>)' method. It refreshes the ribbon UI automatically. |
|
Posted 18 Feb, 2013 05:16:26
|
|
Top
|
|
Bjørn Furuknap
Guest
|
Hi Sergey, and huge thanks for responding.
I had already tried the refresh UI option, but the issue is that I set the Enabled property in the OnActivate method, and refreshing the UI (setEnabled just sets .Enabled and then calls RefreshCommandUI()) then causes the event to fire again, setting off an endless loop.
Am I missing something? Are there other events I can use to the the Enabled state?
The more complete code, including the event handlers and method calls are as such:
ADXTest.Features.SPRibbonTest.SPRibbonTest.prototype.SPRibbonTestButton1OnActivate_EventHandler = f unction (sender) {
sender.Enabled = false;
AllowOnlyCTItemTypes(sender, true);
};
function AllowOnlyCTItemTypes(button, allowFolders) {
[...]
ctx.executeQueryAsync(Func tion.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
function onQuerySucceeded() {
if (some_Condition) {
button.Enabled = true;
// The next line uncommented causes endless loop and crashes client
// button.setEnabled();
}
}
}
Thanks again, I greatly appreciate your response.
.b |
|
Posted 19 Feb, 2013 07:28:27
|
|
Top
|
|
Sergey Grischenko
Add-in Express team
Posts: 7233
Joined: 2004-07-05
|
Hi Bjørn,
Please try to change the code as shown below. It should avoid the endless loop.
var lockRefresh = false;
ADXTest.Features.SPRibbonTest.SPRibbonTest.prototype.SPRibbonTestButton1OnActivate_EventHandler = f unction (sender) {
if (!lockRefresh) {
sender.Enabled = false;
AllowOnlyCTItemTypes(sender, true);
}
};
function AllowOnlyCTItemTypes(button, allowFolders) { [...] ctx.executeQueryAsync(Func tion.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
function onQuerySucceeded() {
if (some_Condition) {
lockRefresh = true;
button.setEnabled();
lockRefresh = false;
}
}
}
|
|
Posted 19 Feb, 2013 10:59:42
|
|
Top
|
|
Bjørn Furuknap
Guest
|
Thanks again, Sergey,
For some weird reason, your code does not work because for whatever reason, when I run the setEnabled method, the next time the OnActivate handler runs, the .Enabled is undefined.
However, you got me on the right track so I simply did
lockRefresh = true;
button.Enabled = true;
RefreshCommandUI();
lockRefresh = false;
and that worked as expected.
Thank you for great support!
.b |
|
Posted 19 Feb, 2013 11:22:42
|
|
Top
|
|
Sergey Grischenko
Add-in Express team
Posts: 7233
Joined: 2004-07-05
|
Hi Bjørn,
Thank you for the solution. Let me know if you face any other difficulties. |
|
Posted 20 Feb, 2013 04:07:50
|
|
Top
|
|