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)          {                 "`t  `t $($Group.Name) `t   `t `t " | out-file "E:\Ulhas\UsersandGroupsRpt.txt" -append                 #Iterate throu

GetListItem and UpdateListItem using SPServices in MOSS 2007

GetListItem and UpdateListItem using SPServices in MOSS 2007 $(document).ready(function() { var itemToUpdate = GetListItem(); UpdateListItem(itemToUpdate); }); function GetListItem(){ var today = new Date(); var dayOfWeek = today.getDay(); var itemID = 0; var out = ""; var titleField = "<FieldRef Name='Title' />"; var descriptionField = "<FieldRef Name='Description' />"; var viewFields = "<ViewFields>" + titleField + descriptionField + "</ViewFields>"; var myQuery = "<Query>" + "<OrderBy>" + "<FieldRef Name='ItemOrder' Ascending='True'/>" + "</OrderBy>" + "</Query>"; $().SPServices({ operation: "GetListItems", async: false, listName: "MyList", CAMLQuery: myQuery,   CAMLViewFields: viewFields, completefunc: fun

Bind a html string with Enhanced rich text box which has heavy formatting in O365 using Angular JS

Bind a html string with Enhanced rich text box which has heavy formatting in O365 using Angular JS , below solution did the trick. You have to use  $sce.trustAsHtml() , to use it directly into the DOM, you could do it like this, JS/controller part: Ref:  http://stackoverflow.com/questions/21503588/angularjs-bind-html-string-with-custom-style $scope . trustAsHtml = function ( string ) { return $sce . trustAsHtml ( string ); }; And in DOM/HTML part <div data-ng-bind-html = "trustAsHtml(htmlString)" ></div>