Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

How can I change EnabledScript asynchronously? I need this, because EnabledScript depended in my case on some item fields, but I can read them only in async way:

var itemId = SP.ListOperation.Selection.getSelectedItems()[0].id;
var clientContext = SP.ClientContext.get_current();
var list = clientContext.get_web().get_lists().getById(listId);
this.listItem = list.getItemById(itemId);
clientContext.load(this.listItem, 'MyPropertyForDisabling');
clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded),
                          Function.createDelegate(this, onQueryFailed));

and what I need to do than query's extract data?

function onQuerySucceeded() {
 //change EnabledScript status
}

Thanks!

share|improve this question

3 Answers 3

I see you've read the article on the Microsoft SharePoint Team Blog on this topic. I'm not sure I can give you a definitive answer, but here are my thoughts:

  • Effectively it seems you're trying to find how to interact with the ribbon based on client-side code (e.g. Client OM). I also looked at this for quite a long time, and wrote down some thoughts in Customize the ribbon programmatically from web parts and field controls
  • Perhaps you should approach this from another angle. The ribbon framework decides when to call 'EnabledScript' (i.e. based on what the user clicks), so if you can get your client-side code to run in the EnabledScript block, you might be able to achieve what you're trying to do.
share|improve this answer
    
Chris, thanks! But code in EnabledScript block wait for boolean return in sync way. Variant with sync wrapper (async+while) for return value get the high overhead. Continue research –  Vitaly Baum Feb 23 '10 at 16:45
    
Yes I agree - using the Client OM with it's async model can be difficult in the ribbon framework. In one of my samples on my blog I had to move some Client OM code to ensure it executes and gets the return value before the ribbon framework calls the code which requires it. Fun! –  Chris O'Brien - MVP Feb 23 '10 at 17:17
    
I watched your samples, great work! Interested, what happend if I declare more than one CommandUIHandlers on one command –  Vitaly Baum Feb 23 '10 at 18:02
    
Hmm, I'm not sure! I think they might both get executed, as I think I saw a JavaScript array which holds the command names. Not 100* sure though, you'd have to test. –  Chris O'Brien - MVP Feb 23 '10 at 22:11
    
Chris, I close to the solution, do you know how to get CUI.Controls.Button form Ribbon by id? –  Vitaly Baum Feb 27 '10 at 18:24

When using async calls to check if a button has to be enabled in the ribbon you have to tell the ribbon when the async method is finished to check if the status of the button has to be updated. The RefreshCommandUI method helps you out with asynchronous calls. Besides this method a variable has to be set to keep track of the status(enable or disable) of the button.

I happened to write a post about it which you can find here: http://www.itidea.nl/index.php/using-async-call-to-enable-a-custom-ribbon-button/

Regards, Anita

share|improve this answer

Check out the SharePoint Blog on how to do this here: http://blogs.msdn.com/b/sharepointdev/archive/2011/02/17/using-refreshcommandui-with-the-server-ribbon.aspx

Just hook this up in a script to your EnabledScript attribute on your ribbon button and you should be good to go.

It works by issuing the async call first, then processing the results in a timer that gets shut off when it's done working. Not very elegant but gets the job done.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.