2
votes

I have a SharePoint 2013 (The Cloud version) custom list where 1 column is a text field where contact numbers are keyed in. How can I get SharePoint to highlight duplicate values in that column so that every time a new item is added to the list, I'll know if the contact number has been used previously?

Ideally, here's what I'd get if I were to enter 816's details for the 2nd time:

CNO....Name.......Issue
816.....Blink........Login Problem (highlighted in red)
907.....Sink.........Access Denied
204.....Mink.........Flickering Screen
816.....Blink........Blank Screen (highlighted in red)

I've been struggling with this for awhile and would be very grateful for any advice. Thanks!

2

2 Answers

2
votes

Since SharePoint 2013 uses Client Side Rendering (CSR) as a default rendering mode I would recommend the following approach. Basically the idea is to customize List View on the client side as demonstrated below.

Assume the Requests list that contains RequestNo column.

The following JavaScript template is intended for highlighting the rows when list item with RequestNo column occurs more then once:

SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
  OnPostRender: function(ctx) {
    var rows = ctx.ListData.Row;

    var counts = getItemCount(rows,'RequestNo');  //get items count    

    for (var i=0;i<rows.length;i++)
    {
      var count = counts[rows[i]["RequestNo"]];
      if (count > 1) 
      {
        var rowElementId = GenerateIIDForListItem(ctx, rows[i]);
        var tr = document.getElementById(rowElementId);
        tr.style.backgroundColor = "#ada";
      }
    }
  }
});


function getItemCount(items,propertyName)
{
   var result = {};
   for(var i = 0; i< items.length; i++) {
      var groupKey = items[i][propertyName];
      result[groupKey] = result[groupKey] ? result[groupKey] + 1 : 1;
   }
   return result;
}

How to apply the changes

Option 1:

Below is demonstrated probably one of easiest way how to apply those changes:

  1. Open the page in Edit mode
  2. Add Content Editor or Script Editor web part on the page
  3. Insert the specified JavaScript template by enclosing it using script tag into web part

Option 2:

  1. Save the specified JavaScript template as a file (let's name it duplicatehighlight.js) and upload it into Site Assets library
  2. Open the page in Edit mode and find JSLink property in List View web part
  3. Specify the value: ~sitecollection/SiteAssets/duplicatehighlight.js and save the changes.

Result

enter image description here

1
votes

SharePoint has some basic conditional formatting for Data View Web Parts and XSLT List Views, but the conditions you can use are rather limited. You can compare a field in the current item with a value that you specify. There are no formulas to count the number of items with the same name or similar, which would be the approach to use to identify duplicates.

If you need to identify duplicates, you may want to create a view that groups by the CNO number. Grouping will also include an item count, so you can run down the list and spot groups with more than one item.