Thursday, November 17, 2016

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>

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....