Learning SharePoint   Enhance Your SharePoint Knowledge

Subscribe to Learning SharePoint Subscribe
Twitter Learning SharePointFollow Us
in Learning SharePoint
Check Out Our New SharePoint 2013 series!

Enable\Disable Ribbon button by Users Group Sharepoint 2010

06/20/2011  
Category: SharePoint 2010, SharePoint Programming


In this post we will see a detailed example of how to enable and disable a ribbon button according to the Logged in user’s group. The idea here is to enable the ribbon button if the current user is a part of a pre-specified group (say ListOwners) and disable the same if the user is not one of the added users of the group.

The example uses “EnabledScript” attribute of the CommandUIHandler of the ribbon button to decide whether the button should be enabled or disabled for the loggedin user.

Code Overview -

1.  The below example enables the ribbion button if the current user is a part of a pre-specified group (say ListOwners)

2. The button remains\gets disabled if the user does not exist in the specified group.

3. A separate JavaScript file CheckUserInGroup.js which is deployed in /_layouts/RibbonScripts contains the EnableIfUserInGroup() function. This function is called in EnabledScript and it executes to check if the user exists in the pre-specified group.

Lets Start with creating a Ribbon button first  -

Steps -

1. Create a empty project.

2. Deploy it as a Farm solution.

3. Right click on the feature and click “Add feature”.

4. Right click on the project and add a new “Empty Element” item.

5. Next add the below code to add a custom Ribbon button to your document library.

<Elements xmlns=”http://schemas.microsoft.com/sharepoint/” >
<CustomAction
Id=”ButtonForGroupUsersOnly”
Location=”CommandUI.Ribbon”
RegistrationId=”101″
RegistrationType=”List”
Title=”Owners Group Button”>
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition
Location=”Ribbon.Library.ViewFormat.Controls._children”>
<Button Id=”Ribbon.Library.ViewFormat.UsersBtn”
Command=”usersBtnCommand”
LabelText=”Group Users Button”
Image32by32=”/_layouts/1033/IMAGES/buttonIcon.jpg”
TemplateAlias=”o1″ />
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler
Command=”usersBtnCommand”
CommandAction=”javascript:OwnerBtnscript();“/>  –> Refer Your Function here. This runs after your button is clicked
EnabledScript=”javascript:EnableIfUserInGroup();” -> Enable Ribbon function here
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>

//Referencing the Script
<CustomAction
Id=”OwnersButton.Script”
Location=”ScriptLink”
ScriptSrc =”/_layouts/RibbonScripts/CheckUserInGroup.js”/>
</Elements>

The Group Users Button created in the above code will be in the disabled mode on page load. The code in EnableIfUserInGroup(); will determine if the current  user is added to the specified group and the button needs to be enabled.

The CustomAction ScriptLink refers to the path of the CheckUserInGroup.js file which contains EnableIfUserInGroup(); and other JavaScript functions.

Next  Lets take a Look at the Code for CheckUserInGroup.js and EnableIfUserInGroup(); >>

Subscribe to Learning SharePoint Subscribe
Twitter Learning SharePointFollow Us
in Learning SharePoint
Advertisement

Random Posts

Set People and Group Field with Current User’s Login Name using Ecmascript\Javascript using Client Object Model – SharePoint 2010
Set People and Group»

Here is a quick example of  how to set the People and Group (People Picker) Field with the Login Name»

Get item version Ecmascript client object model SharePoint 2010
Get item version Ecmascript»

Here is a short example about how to retrieve the current version if an item using EcmaScript\Javascript client object model.»

Get values from multichoice column client object model sharepoint 2010
Get values from multichoice»

A short code snippet about how to add and retrieve values from multichoice column using client object model. ListItemCollection listItems»

Recent Forum Posts

Linq to Sharepoint

by RS on May 18, 2012

comments on the code

by spbsmile on May 18, 2012

connection(add) EditControlBlock and my program

by spbsmile on May 18, 2012

Custom master pages used in multiple SharePoint 2010 sites

by ubersteve on May 18, 2012

upload file and click on button but cant upload file into list by using below code

by santhoshreddy on May 18, 2012