Wednesday, February 24, 2010

How to: Enabling a Button on the Ribbon Based on Selection

Hi all, 

Today I read Microsoft SharePoint Team Blog where author showed how to create a button which enables if and only if a single item is selected.Its not a out of the box SharePoint 2010 feature, but it can be achieved very easily using ribbon control and javascript. He has explained very neatly, I will just walk-through. 

Step1: We first create a New Project in Visual Studio 2010  Beta 2/RC >> Choose "SharePoint 2010" as base template >> select "Empty SharePoint Project" >> Name "RibbonBasedSelection" and click OK.
Step2: Right click on "RibbonBasedSelection" >> Add >> New Item >> Under SharePoint 2010 template create a empty element named "RibbonBasedSelection"


Step3. We add a CustomAction which is root of server ribbon control. Later we deal with CommandUIExtension, CommandUIDefinitions where specifically define the location of the command (Location=Ribbon.Documents.New.Controls._children). In "CommandUIHandler" we define the "CommandAction" to show to the users that they can select only one time. We also "EnabledScript" which has a function "singleEnable()". JavaScript uses SharePoint client model to retrieve one time. 


Step4: Build and Deploy the solution.


Step5: Navigate to "Shared Documents" >> click on "Document" tab. We can see the "Single Select". Please note that button is disabled as we have not selected any document. This shows that Ribbon is contextual. 



Now, select the a document. The "Single Select" button is enabled. 

When we click on the "Single Select" button, a javascript pops up showing the "There is one thing selected!" 


If we two documents, then "Single Select" button is disabled again. 

Cheers, 
--aaroh

Download the source here.

No comments:

How to display Sharegate administrative dynamic reports via Power BI on SharePoint Online

After working in the SharePoint domain for a couple of years as SharePoint consultant , I am fortunate enough to help customers from man...