First of all you need a custom action. If you need to hide a button on a list view or item view - use CommandUI.Ribbon as location. If you need to hide a button in a new form, edit form or view form - use NewFormToolbar, EditFormToolbar or DisplayFormToolbar as location. Full list of default ribbon locations here and customization locations here).
Add a class which inherits the "Control" class. Then you handle the OnLoad event of the custom action you created:
protected override void OnLoad(EventArgs e)
this.Visible = false;
if (Microsoft.SharePoint.SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
SPRibbon ribbon = SPRibbon.GetCurrent(this.Page);
// check if current URL has a path of specific folder, etc.
// Try using:
// SPContext.Current.File.Url or HttpContext.Current.Request.Url
You hide ribbon buttons by using this code:
You can get the IDs of the Ribbon buttons using IE developer tool (F12):
In a nutshell it works like this: you add a custom action to a location where you want to run some code behind. Every time you access that location (open a new form, go to list settings etc.) that custom action execute ControlClass methods (OnLoad, OnRender etc.). This is the place where you want to do all your checks and hide you buttons.
In case you want to disable the button based on conditions, you still follow these instructions and after hiding the button, you add it again, except you don't specify any command. Since no command will be associated with it - it will be grayed out.
Take a look at this pseudo code:
var web = SPContext.Current.Web;
var list = web.Lists.TryGetList("RibbonButtonList");
if (list != null)
var userAction = list.UserCustomActions.Add();
userAction.Location = "CommandUI.Ribbon";
userAction.ImageUrl = "/_layouts/images/image.png";
userAction.CommandUIExtension = @"<CommandUIExtension>
Alt=""Programmatically Added Button""
LabelText=""Programmatically Added Button""
Hope this helps.