Press "Enter" to skip to content

How to hide Edit Page button from Site Actions menu of SharePoint Publishing Site

This post is related to SharePoint 2010 / 2013 and describes steps to hide “Edit Page” or any other button from SharePoint Site Actions menu for publishing layout / page in a custom solution.

 

There might be a case where your client may not want the default.aspx page to be editable for example. So I had requirement to hide the “Site Actions Menu” -> “Edit Button” for default.apsx page if the user is not owenr of the current SharePoint web.

For that purpose custom SharePoint solution is created where the default.aspx for example is overwritten and inherits from custom class:

The class:

namespace YourProject.News.WebPartPage
{
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;

    /// <summary>The web part page ex.</summary>
    public class WebPartPageEx : WebPartPage
    {
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
            var currentWeb = SPContext.Current.Web;
            var currentUser = SPContext.Current.Web.CurrentUser;

            //Show ribbon is Owner or Site Admin
            if (!currentUser.IsSiteAdmin && !currentWeb.DoesUserHavePermissions(currentUser.LoginName, SPBasePermissions.ManageSubwebs))
            {
                SPRibbon ribbon = SPRibbon.GetCurrent(this.Page);
                if (ribbon != null)
                {
                    ribbon.CommandUIVisible = false;
                }
                
                SiteActions actions = SiteActions.GetCurrent(this.Page);
                if (actions != null)
                {
                    //The MenuItem_EditPage is the Edit page button
                    //For other button name reference search : 
                    //15\TEMPLATE\LAYOUTS\EDitingMENU\SiteAction.xml or 15\TEMPLATE\CONTROLTEMPLATES\PublishingActionMenu.ascx
                    actions.GetMenuItem("MenuItem_EditPage").Visible = false;
                }
            }
        }
    }
}

This is how the default.aspx or other publishing page / page layout can inherit from this class:

<%@ Page Language="C#" MasterPageFile="~masterurl/custom.master" Inherits="YourProject.News.WebPartPage.WebPartPageEx, YourProject.News, Version=1.0.0.0, Culture=neutral, PublicKeyToken=846e17f7d8988c09" meta:progid="SharePoint.WebPartPage.Document" meta:webpartpageexpansion="full" %>
Cheers