1

I have the following folder structure in my document library:

Library
  |----- Folder 1
  |         |----- Sub-folder 1
  |
  |----- Folder 2
            |----- Sub-folder 2

I have a requirement in which when user is at library level, some of the ribbon buttons are disabled (aka grey out) but not at folder 1 level (where user is looking at sub-folder 1 or sub-folder 2).

I understand that it can be done via javascript or C# but the examples I found don't really meet my requirement.

How can I go about doing this?

1
  • You can try to use the EnabledScript option. You can try to identify in which folder you are currently in and disable the button. You can refer this question which details out "How to find the current folder through JS COM?" Not sure if this will work or not as I haven't tried it myself.
    – Naveen
    Mar 16, 2015 at 10:50

1 Answer 1

1

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).

<CustomAction Id="DMS.Lists.HideButtonCustomAction" Location="CommandUI.Ribbon" RegistrationType="List" RegistrationId="1005590" ControlAssembly="$SharePoint.Project.AssemblyFullName$" ControlClass="DMS.Lists.HideButtonCustomActionCodeBehind"> </CustomAction>

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)
{
    base.OnLoad(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
        HideCustomActions(ribbon);
    }
}

You hide ribbon buttons by using this code:

ribbon.TrimById(buttonToTrim);

You can get the IDs of the Ribbon buttons using IE developer tool (F12): enter image description here

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>
     <CommandUIDefinitions>
     <CommandUIDefinition Location=""Ribbon.List.Settings.Controls._children"">
     <Button
     Id=""{5557CC45-324E-41bb-9E88-A535DBF1BF6B}""
     Alt=""Programmatically Added Button""
     Sequence=""5""
     Command=""""
     Image32by32=""/_layouts/images/menulistsettings.gif""
     Image16by16=""/_layouts/images/itdcl.gif""
     LabelText=""Programmatically Added Button""
     TemplateAlias=""o1"" />
     </CommandUIDefinition>
     </CommandUIDefinitions>
    <CommandUIHandlers>
    </CommandUIHandlers> 
   </CommandUIExtension>";
    userAction.Update();
    }

Hope this helps.

2
  • Thank you for the informative answer, it is useful. However, my requirement is to disable (aka grey out) the button(s) instead of hiding them. Any ideas on that?
    – Zerhinne
    Mar 16, 2015 at 13:47
  • I edited my asnwer since my reply was too long for a comment. Otherwise you can disable the button using JS. The approach is a matter of preference. Mar 16, 2015 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.