Implementing Mark As Read and Delete functionality for selected list items in SharePoint 2013


We recently had a requirement to implement Mark As Read, Unread and Delete feature for the list items selected in the list.

User will select the list items record and can perform the required operation by selecting appropriate button.

For this we added a content editor web part having the following content


<div>
 <input type="button" value="Mark As Read" onclick="javascript:MarkRead();" />
 <input type="button" value="Mark As UnRead" onclick="javascript:MarkUnRead();" />
 <input type="button" value="Delete" onclick="javascript:Delete();" />
</div>

<script language='javascript' type='text/javascript'>

var items;
 var ctx;
 var count;
 var web;
 var totalCount = 0;

function Delete() {
 debugger;
 ctx = SP.ClientContext.get_current();
 web = ctx.get_web();
 var currentlibGuid = SP.ListOperation.Selection.getSelectedList();
 if (currentlibGuid == null) {

alert('Please select an item in the list!');
 return;
 }
 var currentLib = web.get_lists().getById(currentlibGuid);
 items = SP.ListOperation.Selection.getSelectedItems(ctx);

//Get Selected Items count
 count = CountDictionary(items);

&nbsp;

for (var i in items) {
 var currentItem = currentLib.getItemById(items[i].id);
 ctx.load(currentItem);

currentItem.deleteObject();

ctx.executeQueryAsync(
 Function.createDelegate(this, this.Succeeded),
 Function.createDelegate(this, this.onQueryFailed)
 );

}
 }

function MarkUnRead() {
 ctx = SP.ClientContext.get_current();
 web = ctx.get_web();
 var currentlibGuid = SP.ListOperation.Selection.getSelectedList();
 if (currentlibGuid == null) {

alert('Please select an item in the list!');
 return;
 }
 var currentLib = web.get_lists().getById(currentlibGuid);
 items = SP.ListOperation.Selection.getSelectedItems(ctx);

//Get Selected Items count
 count = CountDictionary(items);

&nbsp;

for (var i in items) {
 var currentItem = currentLib.getItemById(items[i].id);
 ctx.load(currentItem);

currentItem.set_item('MarkAsRead', false);
 currentItem.update();

ctx.executeQueryAsync(
 Function.createDelegate(this, this.Succeeded),
 Function.createDelegate(this, this.onQueryFailed)
 );
 }
 }
 function MarkRead() {

ctx = SP.ClientContext.get_current();
 web = ctx.get_web();
 var currentlibGuid = SP.ListOperation.Selection.getSelectedList();
 if (currentlibGuid == null) {

alert('Please select an item in the list!');
 return;
 }
 var currentLib = web.get_lists().getById(currentlibGuid);
 items = SP.ListOperation.Selection.getSelectedItems(ctx);

//Get Selected Items count
 count = CountDictionary(items);

&nbsp;

for (var i in items) {
 var currentItem = currentLib.getItemById(items[i].id);
 ctx.load(currentItem);

currentItem.set_item('MarkAsRead', true);
 currentItem.update();

ctx.executeQueryAsync(
 Function.createDelegate(this, this.Succeeded),
 Function.createDelegate(this, this.onQueryFailed)
 );
 }
 }
 function onQueryFailed(sender, args) {

totalCount = totalCount + 1;

if (totalCount == count) {
 location.reload();
 }
 alert('Request failed. ' + args.get_message() +
 '\n' + args.get_stackTrace());
 }

function Succeeded() {

totalCount = totalCount + 1;

if (totalCount == count) {
 location.reload();
 }
 }
</script>

&nbsp;

Was helped by a good friend of mine and colleague Priyanka, a SharePoint expert. You can check her insightful posts at her MSDN Blog

http://blogs.msdn.com/b/mind_talks/

Hope it helps


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

One thought on “Implementing Mark As Read and Delete functionality for selected list items in SharePoint 2013”

  1. Hi, and thank you for this. I know this is an older post, but it is the only one together with another one from the same author. But I can’t seem to get it to work. I get the buttons but it doesn’t change the marking for read or unread – I allready made the column.

    Like

Leave a reply to Line T Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.