Thursday, November 4, 2010

Hiding “New Site” in Site Actions Menu

Today, I came across an interesting question in a forum and the question was.. “How to hide “New Site” option available in the Site Actions menu when the publishing feature is enabled.

Initially, I thought it would be easy with HideCustomAction element in a custom feature. However, it did not turn out to be that easy. After some investigation I figured out that that menu item is created by Publishing feature in code. HideCustomAction would have helped me if it was created declaratively. So, I took the same approach as Publishing feature to hide the menu. Here is the solution:

This was tough when I started doing it today. It is possible via feature using a CustomAction element in the elements.xml and some code. I tried a no-code solution but it was not possible.

 <CustomAction
        Id="HideCreateSiteInSiteActions"
        Location="Microsoft.SharePoint.StandardMenu"
        GroupId="SiteActions"
        Sequence="901"
        Title="Publishing SiteActions Menu Customization"
        ControlAssembly="Your Assembly full name"        ControlClass="your class full name">
  </CustomAction>

Following code would go in class:

public class YourClassName : WebControl
    {
        protected override void OnLoad(EventArgs e)
        {
            var siteActions = ToolBarMenuButton.GetMenuControl(this);
            siteActions.MenuControl.PreRender += (o, args) =>
            {
                var targetMenu = siteActions.GetMenuItem("MenuItem_CreateSite");
                if (targetMenu == null) return;
                siteActions.MenuControl.HiddenMenuItems.Add(targetMenu);
            };
        }       
    }

The scope of your feature would be "site". Also, don't forget to put SafeControl entry for your class in the web.config or it may not work.

No comments:

Post a Comment