How to Hide Publishing Site Actions Menu Links

July 15 2010 101 comments

One of our clients requested us to customize SharePoint in a way that we needed to hide/replace SharePoint Site Actions menu’s New Page -link. Well, at first we thought that this is easy task to do, but it wasn’t – until we figured out how to do it.

We googled a little bit, and found this thread:
SharePoint – Hide items in ‘Site Actions’ menu

Unfortunately the thread didn’t give us an answer that we could use. Well, there is a one answer: customization of “_catalogs/masterpage/Editing Menu/CustomSiteActions.xml”. Usually, we do not want to touch this file. It’s something that is better to be left for a customer to customize. And in this case we wanted to include this new feature in our product. Modifying CustomSiteAction.xml in our product essentially makes that file our property, and that’s not what we wanted.

But before we go to details, let’s look how Microsoft implemented their customizations in their Publishing feature.

Usually we can use <CustomAction> and <HideCustomAction> to add and remove links and buttons from SharePoint GUI. But Microsoft decided not to eat their own dog food in publishing actions. Wrong decision, if you ask us. Their intention for doing it differently in this case, was probably because they wanted to give a possibility to modify actions with SharePoint Designer (and that’s why they added the CustomSiteAction.xml, and the others). I don’t have internal information about this, it’s just a guess. Microsoft’s implementation wasn’t right on the spot.

How then Microsoft does this?

First of all, it all starts from FEATURES\Publishing. In that folder, let’s look at SiteActionMenuCustomization.xml:

<CustomAction
 Id="PublishingSiteActionsMenuCustomizer"
 Location="Microsoft.SharePoint.StandardMenu"
 GroupId="SiteActions"
 Title="Publishing SiteActions Menu Customization"
 ControlAssembly="Microsoft.SharePoint.Publishing, ..."
 ControlClass="...WebControls.PublishingSiteActionsMenuCustomizer">
</CustomAction>

This is all fine and standard way to do it in SharePoint. But this isn’t a single action. It’s a class that creates whole lot of them. And that’s why we can only hide them all (with PublishingSiteActionsMenuCustomizer). But we needed to hide only one of them, as stated earlier.

How does this PublishingSiteActionsMenuCustomizer -class work?

It’s hard coded in this sealed class that it reads a file called:

14\TEMPLATE\LAYOUTS\EditingMenu\SiteAction.xml

If you look at this file, you see that it’s a collection of Console Nodes. For example:

<ConsoleNode
 Action="cms:CreateNewPublishingPageAction"  
 HideIfDisabled="true"  
 UseResourceFile="true"  
 MenuGroupId="200"  
 Sequence="210"  
 PermissionContext="CurrentList"  
 ImageUrl="/_layouts/images/crtpage.gif"  
 ID="wsaCreatePage"
 UIVersion="4"
 ConfigMenu="Replace"
 ChangedNodeID="MenuItem_CreatePage"/>

This file basically replicates CustomAction and HideCustomAction -tags, but in an obfuscated way. As you can see from the above XML, this particular console node replaces normal “MenuItem_CreatePage” action with different implementation. It kinda replaces “New Wiki Page” with “New Publishing Page”. It also changes the id (from “MenuItem_CreatePage” to “wsaCreatePage”). And that’s why hiding “MenuItem_CreatePage” hasn’t any effect. Hiding “wsaCreatePage” (with HideCustomAction) doens’t work either, because these are created thought code later in the page life cycle, and SharePoint doesn’t know anything about them.

The Solution

First we created a new Custom Action with a Feature, here is our Custom Action:

<CustomAction
 Id="CreatePublishingPageCustomization"
 Title="Publishing Site Actions Menu Customization"
 Description="Replaces New Page action with a more intelligent one. "
 Location="Microsoft.SharePoint.StandardMenu"
 GroupId="SiteActions"
 ControlAssembly="SharePoint.Accelerator.Web.UI..."
 ControlClass="...CreatePublishingPageCustomization">
</CustomAction>

And in the code we did write this:

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

Okay, that looks easy now, but trust me, we looked at many many possibilities before finally found it. We needed to use MenuControl.PreRender, because otherwise the “wsaCreatePage” wasn’t there yet. Microsoft adds their publishing actions at PreRender.

Please let us know, if you did found this useful!

Popularity: 5% [?]

101 comments to “How to Hide Publishing Site Actions Menu Links”

  1. Teespring says:

    Do you mind if I quote a couple of your articles
    as long as I provide credit and sources back to your site? My blog site is in the exact same area of interest as yours and my visitors would truly benefit from some of the information you
    provide here. Please let me know if this okay with
    you. Regards!

Leave a Reply