none
Enable the ribbon's button in a specific Document Library

    Question

  • Hello,

    I want to make a button that is enabled only in a specific document library (such as "Shared Docs").

    Now I try to use javascript in "EnabledScript" attribute to do this:

    function isInSharedDocs() {
            var clientContext = SP.ClientContext.get_current();
            var listId = SP.ListOperation.Selection.getSelectedList();
            var colList = clientContext.get_web().get_lists();
            this.oList = colList.getById(listId);
            clientContext.load(oList);
            clientContext.executeQueryAsync(onsuccess, onfailure);
        }
    
    function onsuccess(){
            var listTitle = this.oList.get_title();
            return (listTitle === "Shared Docs");
    }
    
    function onfailure(){
    	return false;
    }

    Element.xml

            <CommandUIHandler Command="Mytab.MyButton"                       CommandAction="javascript:mytab_mybutton();"
                              EnabledScript="javascript:isInSharedDocs();"/>

    But this does not work as expected.

    Hope someone helps me.



    • Edited by Mao Zhi Thursday, March 8, 2012 1:14 PM
    Thursday, March 8, 2012 1:10 PM

Answers

  • I don't think your code can work by using a asynchronous function "isInSharedDocs". Try this code (I didn't test it):

    var isEnabled = true;
    var isChecked =false;
    
    function isInSharedDocs() {
            if(isChecked) return isEnabled;
            var clientContext = SP.ClientContext.get_current();
            var listId = SP.ListOperation.Selection.getSelectedList();
            var colList = clientContext.get_web().get_lists();
            this.oList = colList.getById(listId);
            clientContext.load(oList);
            clientContext.executeQueryAsync(onsuccess, onfailure);        
        }
    
    function onsuccess(){
            var listTitle = this.oList.get_title();
            isEnabled = (listTitle === "Shared Docs");
            isChecked = true;
            RefreshCommandUI(); // Force a refresh of the Ribbon
      }
    
    function onfailure(){
    	return false;
    }

    Hope this helps.


    • Edited by ThinkBox Thursday, March 8, 2012 2:28 PM
    • Marked as answer by Mao Zhi Friday, March 9, 2012 1:57 AM
    Thursday, March 8, 2012 2:18 PM

All replies

  • You can do this in code by adding your custom action definition directly to the list's UserCustomActions collection.  This blog post by James Love explains how: http://sp365.co.uk/2011/09/programmatically-add-ribbon-custom-action-to-an-existing-list/

    Stuart

    Thursday, March 8, 2012 1:54 PM
  • I don't think your code can work by using a asynchronous function "isInSharedDocs". Try this code (I didn't test it):

    var isEnabled = true;
    var isChecked =false;
    
    function isInSharedDocs() {
            if(isChecked) return isEnabled;
            var clientContext = SP.ClientContext.get_current();
            var listId = SP.ListOperation.Selection.getSelectedList();
            var colList = clientContext.get_web().get_lists();
            this.oList = colList.getById(listId);
            clientContext.load(oList);
            clientContext.executeQueryAsync(onsuccess, onfailure);        
        }
    
    function onsuccess(){
            var listTitle = this.oList.get_title();
            isEnabled = (listTitle === "Shared Docs");
            isChecked = true;
            RefreshCommandUI(); // Force a refresh of the Ribbon
      }
    
    function onfailure(){
    	return false;
    }

    Hope this helps.


    • Edited by ThinkBox Thursday, March 8, 2012 2:28 PM
    • Marked as answer by Mao Zhi Friday, March 9, 2012 1:57 AM
    Thursday, March 8, 2012 2:18 PM
  • Hello, Stuart.

    The method is a good way, but I don't want to use delegate control right now.

    Thank you for your suggestion.

    Hello, ThinkBox,

    I have to correct a little point in the code you wrote, and it works as expectedly.

    This is the fixed code, for further reference:

    var isEnabled = true;
    var isChecked =false;
    var oList;
    
    function isInSharedDocs() {
            if(isChecked) return isEnabled;
            var clientContext = SP.ClientContext.get_current();
            var listId = SP.ListOperation.Selection.getSelectedList();
            var colList = clientContext.get_web().get_lists();
            oList = colList.getById(listId);
            clientContext.load(oList);
            clientContext.executeQueryAsync(onsuccess, onfailure);        
        }
    
    function onsuccess(){
            var listTitle = oList.get_title();
            isEnabled = (listTitle === "Shared Docs");
            isChecked = true;
            RefreshCommandUI(); // Force a refresh of the Ribbon
      }
    
    function onfailure(){
    	return false;
    }
    Thank you for your reply.
    Friday, March 9, 2012 1:56 AM