3

I have a requirement, an item should be added to a list, in the list web-part it should only display the title field value with unique values.

enter image description here

In the list as shown above, it should display developer team, architech team etc only once.

Is there any jquery to do this?

2
  • Are you looking for a script to run in a page that performs a scan to remove duplicate values in the list web part or you want a script that can be used in a content editor to display list items with unique title values ?
    – Arvi
    Nov 17, 2015 at 7:13
  • I need a script to run in a page that removes duplicate title column values of list items in the list view web part
    – KumarV
    Nov 17, 2015 at 7:18

4 Answers 4

3

You can use CSR instead of jQuery to display duplicate items only once.

In order to do that you have to write some code in

OnPreRender: removeDuplicateByTitle

In the OnPreRender you have to remove duplicates from ctx.ListData.Row. ctx.ListData.Row is an array of objects.

There are many ways to remove duplicates. You can try lodash.

function removeDuplicateByTitle(ctx) {
      ctx.ListData.Row = _.uniq(ctx.ListData.Row, 'Title');
    }

Prerequisite

Add lodash script reference in your master page or in a particular
page. Download it from here

You can use Cisar for CSR. Find full code in JS Bin

View before applying JSLink

enter image description here

View after applying JSLink

enter image description here

See this post by Danny Engelman for using Cisar

4
  • thanks, but how I have to apply this code in SharePoint site.
    – KumarV
    Nov 17, 2015 at 6:53
  • As you say.. It could be bad.. You are doing a for loop inside a forEach with the same array... So for a List with n items this code is looping n * n times (at worst) Nov 17, 2015 at 7:09
  • Yeah I know ...... Trying to fix that and other problem. You can suggest something! @DannyEngelman Nov 17, 2015 at 7:24
  • See my updates now @vikashkumar Nov 17, 2015 at 7:55
3

Have you tried the group-by function in your view? It's not what you aksed for, but maybe it is enough

Go to your list

Click on the thre dots and choose "modify view"

Scroll down to "Group by"

Choose the Column you want to group by (probably "Title" in your case)

0
2

You will be required to follow a 2 stepped approach in order to get unique values from the List.

The stages would be:

  1. Getting all list items from the List
  2. Using LINQ to get unique values from ListItemCollection.

Use the code given below:

//1 Execute query against SP Linq provider   
string keyCol = "SPFieldName";
CamlQuery keyColumnValuesQuery = CamlQuery.CreateAllItemsQuery();
ListItemCollection keyColumnValues = srcDocLib.GetItems(keyColumnValuesQuery);
spContext.Load(keyColumnValues, items => items.Include(item => item[keyCol]), items => items.OrderBy(item => item[keyCol]));
spContext.ExecuteQuery();
//2. Transform LiItemCollection to a List and perform Distinct operation 
var uniqueKeyColumnValues = keyColumnValues.ToList().Select(i => i[keyCol]).Distinct(); 
1
2

As Atish asked for a suggestion in his other answer...

This OnPostRender function filters out the duplicates after the list is displayed.

       var allValues=[];
       ctx.ListData.Row.forEach(function(item){
         var duplicateCheck=item.Title,
             TR=document.getElementById(GenerateIIDForListItem(ctx,item));
         if(allValues.indexOf(duplicateCheck)>-1) TR.style.display='none';
         allValues.push(duplicateCheck);
       });        
  • This code displays the whole List, then hides duplicates.
    It is better to use either the OnPreRender or Item template function to not display duplicates at all.

Update #1

As stated in the comments, you probably want this in the OnPostRender function
because you want your users to see what the Duplicates are

I had to replicate some licensed code to add a Toggle button.
The licensed code is complete OOP and adds a Duplicate filter to all Columns.

This will get you started:

OnPostRender : function () {
    var check = 'Title', //name of Item field to check for duplicates
    all = [], //holds all values
    duplicates = [], //holds all duplicate TR elements, EXCLUDING the first value
    duplicatesShown = true,//will be reset on first init
    button = document.createElement('BUTTON');
    function addToggleButton() {
        button.onclick = function (event) {
            event.preventDefault();
            duplicatesShown = !duplicatesShown;
            duplicates.forEach(function (TR) {
                TR.style.display = duplicatesShown ? 'inherit' : 'none';
            });
            button.innerHTML = (duplicatesShown ? 'Hide' : 'Show') + ' Duplicates';
        };
        document.getElementById('CSRListViewControlDivWPQ2').appendChild(button);
    };
    ctx.ListData.Row.forEach(function (item) {
        if (all.indexOf(item[check]) > -1) {
            var TR = document.getElementById(GenerateIIDForListItem(ctx, item));
            TR.style.backgroundColor = 'pink';
            duplicates.push(TR);
        }
        all.push(item[check]);
    });
    addToggleButton();
    button.click();
}
  • I am still learning every day and copy/paste/pick apart examples as well
    Just realised yesterday all examples out there declare the ctx variable,
    which is totally not required as ctx is a Global object and in JS objects are passed by reference.
    I tried to stick that TR reference on the item but that doesn't work.. Seems SharePoint keeps it as a an immutable object... ah well.. more to investigate

  • If you want to highlight all duplicate values you have to add another loop for all items AFTER the first initialisation
    that is probably why Atish had a loop IN a loop in his original code

ICSR

4
  • In OnPreRender, Still, I can not think anything else n*n looping. I asked you to suggest something about it. Anyway, your answer is also cool. Nov 17, 2015 at 8:07
  • We used OnPostRender so the duplicate items are actually there. Because... we also added a button that Shows/Hides the duplicates.. I can't copy that full code because it is licensed; will have to replicate it and add it to my post... isn't that difficult. Nov 17, 2015 at 8:37
  • yeah please share if possible after replication. Nov 17, 2015 at 8:42
  • In last update, I have replaced loop IN a loop by _.uniq. But I guess, there is also n*n inside _.uniq. Nov 17, 2015 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.