SharePoint 2013: Show/Hide Delete Icon In Ribbon On List Item Selection using jQuery

This SharePoint tutorial explains, how to show/hide the delete icon in list item selection based on the logged-in user using CSOM/REST API and jQuery. The solution will work to Show/Hide Delete Icon In Ribbon On List Item Selection using jQuery in SharePoint Online or SharePoint 2013/2016.

In my previous tutorial, I have explained how to enable or disable the delete icon in Ribbon on list item selection.

The scenario is to disable the delete icon in the ribbon on the SharePoint List item selection. But when any user logs into the SharePoint site the items created by him should have the delete option on them when only one selection is there.

This can be performed using custom Permission Level with Delete permission removed and item-level or folder-level permissions, but this can cause you a lot of problems managing the broken inheritance of permissions. Or you could write an event handler to prevent deletion of that item but the best way to do it is by using jQuery/ CSOM/ REST.

Show/Hide Delete Icon In Ribbon On SharePoint List Item Selection using jQuery

I have offered a code demo about how to Hide the delete icon in the ribbon on List Item Selection in SharePoint.

Before:

show hide delete icon on sharepoint 2016 list item selection

Follow the below steps.

Step 1: Navigate to your SharePoint 2013 site.

Step 2: From this page select the Site Actions | Edit Page.

Edit the page, go to the “Insert” tab in the Ribbon and click the “Web Part” option. In the “Web Parts” picker area, go to the “Media and Content” category, select the “Script Editor” Web Part and press the “Add button”.

Step 3: Once the Web Part is inserted into the page, you will see an “EDIT SNIPPET” link; click it. You can insert the HTML and/or JavaScript as in the following:

show hide delete icon on sharepoint 2013 list item selection
// JavaScript source code
< scripttype = "text/javascript"
src = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js" >
< /script>
< scripttype = "text/javascript" >

$(document).ready(function()
{
ExecuteOrDelayUntilScriptLoaded(init_HideButton, "sp.ribbon.js");
});

function init_HideButton()
{
setInterval(function()
{
HideDeleteIconRibbomButton();
}, 1000);
}

function HideDeleteIconRibbomButton()
{
$('a[id*="Ribbon.ListItem.Manage.Delete-Medium"]').hide(); //Delete Icon Hyper link
var cc = new SP.ClientContext.get_current();
var web = cc.get_web();
var listId = SP.ListOperation.Selection.getSelectedList();
var selectedItems = SP.ListOperation.Selection.getSelectedItems();
if (selectedItems.length == 1)
{
var flag = CheckCreatedAuthor(listId, selectedItems[0].id, _spPageContextInfo.userId);
if (flag) $('a[id*="Ribbon.ListItem.Manage.Delete-Medium"]').show(); //Delete Icon Hyper link
}
else
{
$('a[id*="Ribbon.ListItem.Manage.Delete-Medium"]').hide();
}
}
functionCheckCreatedAuthor(ListId, ItemID, AuthorID)
{
var result = false;
var url = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbyid('" + ListId + "')/items?$filter=((ID eq '" + ItemID + "') and (ApprovalStatus eq 'Draft') and (Author/ID eq '" + AuthorID + "'))";
getListItems(url, function(data)
{
var items = data.d.results;
if (items.length > 0)
{
result = true
}
else
{
result = false;
}
}, function(data)
{
result = false;
});
return result;
}

function getListItems(siteurl, success, failure)
{
$.ajax(
{
async: false,
url: siteurl,
method: "GET",
headers:
{
"Accept": "application/json; odata=verbose"
},
success: function(data)
{
success(data);
},
error: function(data)
{
failure(data);
}
});
} < /script>

Without any list item selection to hide delete icon in the ribbon on List:

show hide delete icon on sharepoint list item selection

With Multi list item selection to hide delete icon in ribbon on List:

show hide delete icon on sharepoint list item selection

With Single list item Selection (Logged in) to show delete icon in the ribbon on List.

show hide delete icon on sharepoint online list item selection

You may like following SharePoint customization tutorials:

I have tried to Hide/Show the delete icon in the ribbon on SharePoint List Item Selection, which will provide you greater flexibility in user interaction on the application. I have achieved this using CSOM/REST API and jQuery in SharePoint 2013. I hope this article is helpful to you.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

Sagar Pardeshi

I am Developer working on Microsoft Technologies for the past 6+years. I am very much passionate about programming and my core skills are SharePoint, ASP.NET & C#,Jquery,Javascript,REST. I am running this blog to share my experience & learning with the community I am an MCP, MCTS .NET & Sharepoint 2010, MCPD Sharepoint 2010, and MCSD HTML 5,Sharepoint 2013 Core Solutions. I am currently working on Sharepoint 2010, MOSS 2007, Sharepoint 2013,Sharepoint 2013 App Dev, C#, ASP.NET, and SQL Server 2008.

>