SharePoint User Custom Actions: Add, Delete and Display user custom action

In this SharePoint 2013 tutorial, I will explain how to work with SharePoint user custom actions. How you can add, delete, and display user custom actions in SharePoint 2013/2016/2019 using the SharePoint server object model.

We will also see, how to delete user custom action using CSOM in SharePoint Online.

SharePoint User Custom Actions

We can use SharePoint user custom actions to extend the standard behavior of SharePoint components. We can extend the user interface by adding user custom actions using the ribbon button.

Here, we will see how to add custom actions in SharePoint using the server object model and using client object model (CSOM).

SharePoint User Custom Actions: Add, Delete and Display user custom action

Now we will see how we can create the visual web part in SharePoint 2013 using Visual Studio.

  • Open Visual Studio, go to File Menu and select a new option from the file menu. From there you can select the project.
  • Now a window will open, there we go to office/SharePoint, click on the tab, it displays options. Select SharePoint Solutions and select SharePoint Empty Project.
  • Enter Name and Location of the Project. Next click on

Next, go to our solution explorer, select the project name and right click on it and select Add -> New Item.

sharepoint user custom actions
sharepoint user custom actions

When we click on a new item it will display the new window, from here you select Visual Web Part (Farm Solutions Only)  and enter the name of the web part and click on Add.

sharepoint online user custom actions
sharepoint online user custom actions

See files added in your solution explorer.

sharepoint user custom action scriptlink
sharepoint user custom action scriptlink

Go to the ascx page, go to the source, there copy this code.

tsinfocustomsolutionUserControl.ascx(Source):

<%@ Assembly Name=”$SharePoint.Project.AssemblyFullName$” %>
<%@ Assembly Name=”Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register Tagprefix=”SharePoint” Namespace=”Microsoft.SharePoint.WebControls” Assembly=”Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register Tagprefix=”Utilities” Namespace=”Microsoft.SharePoint.Utilities” Assembly=”Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Register Tagprefix=”asp” Namespace=”System.Web.UI” Assembly=”System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″ %>
<%@ Import Namespace=”Microsoft.SharePoint” %>
<%@ Register Tagprefix=”WebPartPages” Namespace=”Microsoft.SharePoint.WebPartPages” Assembly=”Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>
<%@ Control Language=”C#” AutoEventWireup=”true” CodeBehind=”tsinfocustomsolutionUserControl.ascx.cs” Inherits=”tsinfocustomsolution.tsinfocustomsolution.tsinfocustomsolutionUserControl” %>
<style type=”text/css”>
.auto-style1 {
width: 41%;
height: 503px;
margin-left:auto;
margin-right:auto;
border-color:darkgreen;
border-width:medium;
border-style:solid;
background-color:goldenrod;
}
.auto-style3{
color:darkgreen;
font-size:large;
}
.auto-style4{
font-weight:bold;
color:ThreeDDarkShadow;
font-size:large;
}
.auto-style5{
text-align: center;
}
</style>
<table class=”auto-style1″ >
<tr>
<td style = “margin-top:auto;text-align:center; font-size:large;color:CaptionText” colspan=”6″ >
<asp:Label ID=”Label2″ runat=”server” text =”Add User Custom Actions To SiteActions” font-bold=”true” ></asp:Label>
</td>
</tr>
<tr>
<td style = “margin-top:auto;vertical-align:top” rowspan=”8″>
<asp:ListBox ID=”ListBox1″ runat=”server” BackColor=”SteelBlue” ForeColor=”ScrollBar” Font-Bold=”true”></asp:ListBox>
</td>
<td class=”auto-style3″>
<asp:Label ID=”Location” runat=”server” Text=”Location”></asp:Label>
</td>
<td>
<asp:TextBox ID=”txtLocation” runat=”server”></asp:TextBox><br />
<asp:RequiredFieldValidator ID=”RequiredFieldValidator1″ runat=”server” ControlToValidate=”txtLocation” ErrorMessage=” Enter Location Here” Font-Bold=”False” Font-Italic=”True” ForeColor=”Red” ValidationGroup=”Group1″></asp:RequiredFieldValidator>
</td>
<td >
&nbsp;</td>
<td style=”font-size:large;color:ThreeDDarkShadow”>
<asp:Label ID=”Label1″ runat=”server” Text=”RemoveLink” Font-Bold=”true”></asp:Label>
</td>
<td>
<asp:DropDownList ID=”DropDownList” runat=”server” BackColor=”WhiteSmoke” Font-Bold=”true”>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class=”auto-style3″>
<asp:Label ID=”Group” runat=”server” Text=”Group”></asp:Label>
</td>
<td >
<asp:TextBox ID=”txtGroup” runat=”server”></asp:TextBox><br />
<asp:RequiredFieldValidator ID=”RequiredFieldValidator2″ runat=”server” ControlToValidate=”txtGroup” ErrorMessage=”Enter Group Name” Font-Italic=”True” ForeColor=”Red” ValidationGroup=”Group1″></asp:RequiredFieldValidator>
</td>
<td >
&nbsp;</td>
<td colspan=”2″ class=”auto-style5″>
<asp:Button ID=”Remove” runat=”server” OnClick=”Remove_Click” Text=”Remove” style=”color:red;font-weight:bold;” BackColor=”GhostWhite”/>
</td>
</tr>
<tr>
<td class=”auto-style3″>
<asp:Label ID=”Sequence” runat=”server” Text=”Sequence”></asp:Label>
</td>
<td >
<asp:TextBox ID=”txtSequence” runat=”server”></asp:TextBox><br />
<asp:RequiredFieldValidator ID=”RequiredFieldValidator3″ runat=”server” ControlToValidate=”txtSequence” ErrorMessage=”*” ForeColor=”Red” ValidationGroup=”Group1″></asp:RequiredFieldValidator>
</td>
<td >
&nbsp;</td>
<td class=”auto-style4″ colspan=”2″ rowspan=”6″>
&nbsp;</td>
</tr>
<tr>
<td class=”auto-style3″>
<asp:Label ID=”Title” runat=”server” Text=”Title”></asp:Label>
</td>
<td >
<asp:TextBox ID=”txtTitle” runat=”server”></asp:TextBox><br />
<asp:RequiredFieldValidator ID=”RequiredFieldValidator4″ runat=”server” ControlToValidate=”txtTitle” ErrorMessage=”Enter Title” Font-Italic=”True” ForeColor=”Red” ValidationGroup=”Group1″></asp:RequiredFieldValidator>
</td>
<td >
&nbsp;</td>
</tr>
<tr>
<td class=”auto-style3″>
<asp:Label ID=”Description” runat=”server”>Description</asp:Label>
</td>
<td >
<asp:TextBox ID=”txtDescription” runat=”server”></asp:TextBox><br />
<asp:RequiredFieldValidator ID=”RequiredFieldValidator5″ runat=”server” ControlToValidate=”txtDescription” ErrorMessage=”Enter Description” Font-Italic=”True” ForeColor=”Red” ValidationGroup=”Group1″></asp:RequiredFieldValidator>
</td>
<td >
&nbsp;</td>
</tr>
<tr>
<td class=”auto-style3″>
<asp:Label ID=”Url” runat=”server” Text=”Url”></asp:Label>
</td>
<td >
<asp:TextBox ID=”txtUrl” runat=”server”></asp:TextBox><br />
<asp:RequiredFieldValidator ID=”RequiredFieldValidator6″ runat=”server” ControlToValidate=”txtUrl” ErrorMessage=”EnterUrl of Site” Font-Italic=”True” ForeColor=”Red” ValidationGroup=”Group1″></asp:RequiredFieldValidator>
</td>
<td >
&nbsp;</td>
</tr>
<tr>
<td class=”auto-style5″ >
<asp:Button ID=”ClearText” runat=”server” OnClick=”ClearText_Click” Text=”ClearText” ValidationGroup=”Group1″ style=”font-weight:bold;color:orangered” BackColor=”GhostWhite”/>
</td>
<td class=”auto-style5″ >
<asp:Button ID=”Add” runat=”server” OnClick=”Add_Click” Text=”Submit” ValidationGroup=”Group1″ style=”color:blueviolet;font-weight:bold;” BackColor=”GhostWhite”/>
</td>
<td style = “text-align: center;” >
&nbsp;</td>
</tr>
<tr>
<td colspan=”2″>&nbsp;</td>
<td >&nbsp;</td>
</tr>
</table>

tsinfocustomsolutionUserControl.ascx.cs:

Below is the SharePoint 2013 server object model code.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
namespace tsinfocustomsolution.tsinfocustomsolution
{
public partial class tsinfocustomsolutionUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetCustomActions();
GetCustomActionsTODropDown();
}
}
void GetCustomActions()
{
SPSite siteCollection = SPContext.Current.Site;
SPWeb web = siteCollection.OpenWeb();
SPUserCustomActionCollection collUserCustomAction = web.UserCustomActions;
foreach (SPUserCustomAction userCustomAction in collUserCustomAction)
{
// Label4.Text += userCustomAction.Title +” “;
ListBox1.Items.Add(userCustomAction.Title);
}
}
void GetCustomActionsTODropDown()
{
SPSite siteCollection = SPContext.Current.Site;
SPWeb web = siteCollection.OpenWeb();
SPUserCustomActionCollection collUserCustomAction = web.UserCustomActions;
foreach (SPUserCustomAction userCustomAction in collUserCustomAction)
{
DropDownList.Items.Add(userCustomAction.Title);
}
}
protected void Add_Click(object sender, EventArgs e)
{
SPSite siteCollection = SPContext.Current.Site;
SPWeb oWebsite = siteCollection.OpenWeb();
SPUserCustomActionCollection collUserCustomAction = oWebsite.UserCustomActions;
SPUserCustomAction oUserCustomAction = collUserCustomAction.Add();
oUserCustomAction.Location = txtLocation.Text;
oUserCustomAction.Group = txtGroup.Text;
oUserCustomAction.Sequence = Convert.ToInt32(txtSequence.Text);
oUserCustomAction.Title = txtTitle.Text;
oUserCustomAction.Description = txtDescription.Text;
oUserCustomAction.Url = txtUrl.Text;
oUserCustomAction.Update();
}
protected void ClearText_Click(object sender, EventArgs e)
{
ClearTextBox();
}
private void ClearTextBox()
{
foreach (Control c in Controls)
{
if (c.GetType() == typeof(TextBox))
{
((TextBox)(c)).Text = string.Empty;
}
}
}
protected void Remove_Click(object sender, EventArgs e)
{
RemoveCustomAction();
GetCustomActionsTODropDown();
}
private void RemoveCustomAction()
{
SPSite siteCollection = SPContext.Current.Site;
SPWeb web = siteCollection.OpenWeb();
SPUserCustomActionCollection collUserCustomAction = web.UserCustomActions;
if (collUserCustomAction != null)
{
foreach (SPUserCustomAction userCustomAction in collUserCustomAction)
{
if (userCustomAction.Title.ToString() == DropDownList.SelectedItem.Value)
{
userCustomAction.Delete();
return;
}
}
}
}
}
}
  • After completion of design and code, Build and deploy the project. This web part is added to Custom Web part under Categories of Web part Page.
  • Create Web part page, open page and next go to page tab, there click on Edit page.
  • Select your solution from custom web part under the categories and click on Add.
sharepoint online custom action using server object model
sharepoint 2013 custom action using server object model

Here enter details and click on submit button.

sharepoint 2013 add user custom actions
sharepoint 2013 add user custom actions

Here the enjoysharepoint is added to Site Actions.

sharepoint 2013 user custom action
sharepoint user custom actions

When the page is loaded usercustomactions are added to dropdown and list box.

display SharePoint 2013 user custom actions
display SharePoint 2013 user custom actions

Remove user custom actions from site action, got to drop down select user custom action and click on the remove button.

delete user custom actions sharepoint 2013
delete user custom actions sharepoint 2013

Check the site action menu, Team announcement is deleted.

This is how we can add, delete and display user custom actions in SharePoint using SharePoint server object model.

Delete SharePoint Online User Custom Actions using CSOM

This SharePoint CSOM tutorial explains, how we can remove or delete UserCustomActions from Ribbon, FormRibbon and ECB Menu using csom .Net managed object model code in SharePoint online.

We will use here C#.Net managed object model code where we will use below dlls:

  • Microsoft.SharePoint.Client.Dll
  • Microsoft.SharePoint.Client.Runtime.Dll

SharePoint delete user custom action using CSOM

Here I have a SharePoint Online list where we have added UserCustomActions buttons to Ribbon, FormRibbon, and to ECB menu.

For one of our requirement, we need to remove the ribbon buttons. So I wrote a console application by using .Net managed object model code.

Below is the full code:

Here we are retrieving the user custom actions for the list by using the below client object model code:

var userCustomActions = list.UserCustomActions;

Then we are calling the DeleteCustomAction method which will check the particular custom action name and then delete it.

public static void DeleteCustomRibbonActionFromList()
{
using (ClientContext ctx = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
{
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
ctx.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
List list = ctx.Web.Lists.GetByTitle("MyTestList");
ctx.Load(list);
ctx.ExecuteQuery();
var userCustomActions = list.UserCustomActions;
ctx.Load(userCustomActions);
ctx.ExecuteQuery();
DeleteCustomAction(userCustomActions, "RibbonDocumentApproval", ctx);
DeleteCustomAction(userCustomActions, "FormRibbonDocumentApproval", ctx);
DeleteCustomAction(userCustomActions, "ECBDocumentApproval", ctx);
}
}
private static void DeleteCustomAction(UserCustomActionCollection userCustomActions, string customActionName, ClientContext ctx)
{
for (int i = userCustomActions.Count - 1; i >= 0; i--)
{
if (userCustomActions[i].Name == customActionName)
{
userCustomActions[i].DeleteObject();
ctx.ExecuteQuery();
}
}
}
private static string GetSPOAccountName()
{
try
{
return ConfigurationManager.AppSettings["SPOAccount"];
}
catch
{
throw;
}
}
private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in ConfigurationManager.AppSettings["SPOPassword"])
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}

Once you run the above code, it will delete the user custom action button from the SharePoint ribbon, form ribbon, and ECB menu from the SharePoint list.

You may like following SharePoint tutorials:

I hope this SharePoint 2013 tutorial explains how to add, delete, and display user custom actions using the SharePoint server object model and also checked, how to delete user custom action using CSOM SharePoint Online.

>