In this SharePoint 2013 tutorial, I will explain how to work with SharePoint user custom action. How you can add, delete, and display user custom actions in SharePoint 2013/2016 using the SharePoint server object model.
Now we will see how we can create the visual web part in SharePoint 2013 using Visual Studio 2017.
- Open Visual Studio 2017, 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.
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.
See files added in your solution explorer.
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 >
</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 >
</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 >
</td>
<td class=”auto-style4″ colspan=”2″ rowspan=”6″>
</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 >
</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 >
</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 >
</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;” >
</td>
</tr>
<tr>
<td colspan=”2″> </td>
<td > </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.
Here enter details and click on submit button.
Here the enjoysharepoint is added to Site Actions.
When the page is loaded usercustomactions are added to dropdown and list box.
Remove user custom actions from site action, got to drop down select user custom action and click on the remove button.
Check the site action menu, Team announcement is deleted.
You may like following SharePoint server object model tutorials:
- Difference between the client-side object model and the server-side object model in SharePoint 2013/2016/Online
- Add, Update, Delete & Display List items in Gridview Programmatically in SharePoint 2016
- Different Ways to enable User Information List in SharePoint 2013
- Get SharePoint content database size using PowerShell and server object model
- Create subsite in SharePoint 2013/2016 programmatically
I hope this SharePoint 2013 tutorial explains how to add, delete, and display user custom actions using the SharePoint server object model.
Hello Everyone!! I am Bhawana a SharePoint MVP and having about 10+ years of SharePoint experience as well as in .Net technologies. I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools. Now exploring more in SharePoint 2016 🙂 Hope here I can contribute and share my knowledge to the fullest. As I believe “There is no wealth like knowledge and no poverty like ignorance”