Show or Hide Ribbon control based on Group Permission

In one of our releases, we came across a requirement to show or hide one of our custom button on ribbon control, based on the groups in which the logged in user is added.

To know, how to add a button on Ribbon control, refer – Adding a button on Ribbon

1. Add a class which will have code for the hiding the button. Lets say, we named the class as “ButtonShowHideDelegate

1. Add a delegate control to the solution from the installed templates as shown below-

Delegate option in Sharepoint Installed templates

Delegate option in Sharepoint Installed templates

2. A settings page will popup for configuring the Delegate Control as shown below –

DelegateControlSettingsClick on the Next button, and then Finish.

3. Open  the “Elements.xml” file and set the value for various properties as shown below –

elements.xmlDescription of above properties,

  • “Id” is the control of MasterPage  which we are replacing using our delegate control.
  • “Sequence” is the sequence set for control, set the value to any value below 100
  • “ControlAssembly” is the full name of the assembly along with the version and PublicKeyToken. You can get the PublicKeyToken from the assembly.
  • “ControlClass” is the class which will host the code to hide button

4. Below is the code to show/hide button on ribbon control based on the users permission

DelegateOnLoadDescription of the above code –

  • OnLoad” event – We have placed our code in this event so as to execute it while page is loading
  • The first “If” condition checks if the current URL is for the list for which we intend to apply the Show/Hide logic.
  • We take the collection of groups for which the current logged in user is a part of.
  • The LINQ statement checks if “sftp Owners” is one out of many groups of the users.
  • If user is not part of “sftp Owners” group, the custom button on ribbon is hidden for the user.

The above step takes care of the coding part of this requirement.

5. The only thing remaining now is adding this .dll to the SafeControls listSafeControlEntryAdd the “dll” to the SafeControl list as shown above. Please note the Safe Controls flag is set to Yes in the above screenshot.

After all the above steps, your delegate control is ready for deployment.