Skip to main content

How to delete duplicates from SharePoint list using JSOM

How to delete duplicates from SharePoint list using JSOM, Credit: Thulasi Pasam Reference: https://community.nintex.com/thread/8516

Note:
  1. Make sure the reference file paths
  2. Make sure the column to test duplicate on CAML query
  3. Update your SharePoint list name

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/tr/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=5, IE=8, IE=9, IE=10" >

</head>
<body>
<div Id="myDiv" style= "width: 75%;">
<table id="tblHeading"  width = "100%" align = "left">
<tr style ="width:100%;">
<td><input type= "button" onclick="Delete()" align = "Center" alt="Delete Duplicates" value="Delete"></td>
</tr>
</table>


</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://sharepoint.jtafla.com/sites/ROI/Scripts/spjs-utility.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.SPServices/0.7.2/jquery.SPServices-0.7.2.min.js" ></script>

<script>
var ListA = new Array();
var ListB = new Array();
var strList= 'Your List Name';

$(document).ready (function() {
GetAllListA();

});

function GetAllListA(){
var vblMembers ='';
var strQuery= "<Query> <OrderBy>  <FieldRef Name='User_Email_Address' Ascending='TRUE' />       </OrderBy>      </Query>";

$().SPServices({
operation: "GetListItems",
async: false,
listName: strList,
CAMLQuery: strQuery,
completefunc: function (xData, Status) {
if(Status == 'success')
{
$(xData.responseXML).SPFilterNode("z:row").each(function(){

ListA.push($(this).attr("ows_User_Email_Address"));
});
}
}
    });

}

function GetAllListB(){
var vblMembers ='';
var strQuery= "<Query> <OrderBy>  <FieldRef Name='User_Email_Address' Ascending='TRUE' />       </OrderBy>      </Query>";
ListB = [];
$().SPServices({
operation: "GetListItems",
async: false,
listName: strList,
CAMLQuery: strQuery,
completefunc: function (xData, Status) {
if(Status == 'success')
{
$(xData.responseXML).SPFilterNode("z:row").each(function(){

ListB.push($(this).attr("ows_User_Email_Address"));
});
}
}
    });

}

function Delete(){
GetAllListA();
GetAllListB();

for (var i=0; i<ListA.length; i++) {
var count = 0;

for (var j=0; j<ListB.length; j++)
{
if(ListA[i] == ListB[j])
{
count = count+1;
if (count > 1)
{
DeleteDuplicate(ListA[i]);
GetAllListB();
count = 0;
break;
}
}
}
if (count > 1)
{
count = 0;
break;
}

}
alert('All duplicates are removed!');
}

function DeleteDuplicate(tempName){
var delId = GetIdByName(tempName);
alert(delId != "undefined")
{
$().SPServices({
operation:"UpdateListItems",
async: false,
batchCmd: "Delete",
listName: strList,
ID: delId,
completefunc:function(xData, Status)
{
   //alert("Item Deleted");
}
});
}
}

function GetIdByName(tempName){
var vblMembers ='';
var strQuery = "<Query><Where><Eq><FieldRef Name='User_Email_Address'/><Value Type='text'>"+tempName+"</Value></Eq></Where></Query>";
    var tempid = new Array();
$().SPServices({
operation: "GetListItems",
async: false,
listName: strList,
CAMLQuery: strQuery,
completefunc: function (xData, Status) {
if(Status == 'success')
{
$(xData.responseXML).SPFilterNode("z:row").each(function(){

tempid.push($(this).attr("ows_ID"));
});
}
}
    });
return tempid[0];
}
</script>
</html>

Comments

  1. Post writing is also a fun, if you know after that you can write if not it is complex to write.

    ReplyDelete
  2. Thanks in favor of sharing such a nice thinking, paragraph is nice, thats why i have read it fully

    ReplyDelete

Post a Comment

Popular posts from this blog

Permission audit report for site and all sub sites using PowerShell

Use below script to get Permission audit report for site and all sub sites using PowerShell, Add-PSSnapin "Microsoft.SharePoint.PowerShell" $URL="https://intranet.contoso.com/sales/mysite/"      $site = Get-SPSite $URL      #Write the Header to "Tab Separated Text File"         "Site Name`t  URL `t Group Name `t User Account `t User Name `t E-Mail" | out-file "E:\Ulhas\UsersandGroupsRpt.txt"          #Iterate through all Webs       foreach ($web in $site.AllWebs)       {         #Write the Header to "Tab Separated Text File"         "$($web.title) `t $($web.URL) `t  `t  `t `t " | out-file "E:\Ulhas\UsersandGroupsRpt.txt" -append          #Get all Groups and Iterate through            foreach ($group in $Web.groups)         ...

Populate people picker field with logged in user in SharePoint using JS

Please use below code to populate people picker in SharePoint online, <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> ExecuteOrDelayUntilScriptLoaded(function () { setTimeout(function () { SetUserFieldValue('AuthorName'); }, 1000); }, 'clientpeoplepicker.js'); //Set PeoplePicker Value function SetUserFieldValue(fieldName) { try{ var userAccountName = _spPageContextInfo.userLoginName; //PeoplePicker Object var peoplePicker = $("div[title='" + fieldName + "']"); //PeoplePicker ID var peoplePickerTopId = peoplePicker.attr('id'); //People PickerEditor var peoplePickerEditor = $("input[title='" + fieldName + "']"); //Set Value to Editor peoplePickerEditor.val(userAccountName); //Get Client PeoplePicker Dict var peoplePickerObject = SPClientPeoplePicke...