How to hide the Site Actions drop down list using JavaScript

When you normally open SharePoint 2010 you will find the Site Action drop down list on the top left:

How to hide the Site Actions drop down list using JavaScript

This post will demonstrate how to hide the Site Actions drop down list using JavaScript.
First, create an empty SharePoint solution using VS and then add a custom action:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="HideSiteActions.SiteActionAddPoint"
GroupId="SiteActions"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="1003"
ControlAssembly="$SharePoint.Project.AssemblyFullName$"
ControlClass="HideSiteActions.SiteActionAddPoint"/>
</Elements>

The ControlClass is defined as follows:

namespace HideSiteActions
{
public class SiteActionAddPoint : Control
{
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
bool HideSiteActions = true;
//check permissions to set HideSiteActions to false or true.
//HideSiteActions = //SPContext.Current.Web.DoesUserHavePermissions(SPBasePermissions.FullMask);
if (HideSiteActions)
{
string JSKey = "JSHideSiteActions";
Type JSType = this.GetType();
string JScript = string.Format("document.getElementById('siteactiontd').style.display = 'none';");
if (!this.Page.ClientScript.IsStartupScriptRegistered(JSType, JSKey))
{
this.Page.ClientScript.RegisterStartupScript(JSType, JSKey, JScript, true);
}
}
}
}
}

Use the “OnPreRender” function to render this java script code: (“document.getElementById(‘siteactiontd’).style.display = ‘none’;”), to hide the Site Actions drop down list.
The variable “HideSiteActions” is used to control Site Actions drop down list’s hide/show based on permissions.

After deploying this feature to SharePoint, refresh your page and the Site Action drop down list will be hidden.

How to hide the Site Actions drop down list using JavaScript

 

Leave a Reply