GetListItems Unique Values

Sep 28, 2011 at 3:15 PM

I realize that CAML does not have a way to get unique values but I'm using the SPServices GetListItems function and I need a way to eliminate the duplicates. What is the best method of doing this that would work well with GetListItems?

Sep 28, 2011 at 5:39 PM

You'll have to create an array, search the array to see if the value exists.  If it doesn't exist inside the array, then add your value into the array.

Depending on the browser(s) you are supporting, .indexOf has native support when searching through arrays.  You can add this functionality into your script anyway, which will allow you to use .indexOf() on arrays, if the browser doesn't support it.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf

Hope that helps,

Matt

Sep 28, 2011 at 6:36 PM

Another option that I have used if I am looping the items is just to add the items to a string and then check them as Matt was saying. This you can do in one pass. If your items are sorted properly you could just set a variable and check it on the next iteration of the loop. Ex would be var unique = $(this).attr("ows_Title") which you set on each pass. Before you set this, test it against the Title field to see if it is the same as the last value. If so, just skip it.

Oct 8, 2011 at 2:35 AM

 

Thank you both for your replies. I wound up doing something similar to what spevilgenius suggested. Since I was taking the CAML query results and writing them to the DOM I was able to set the id of each element to the unique ID of the list result. At the end of the process I added the following script to eliminate any duplicate id's:

$('[id]').each(function() {
var ids = $('[id='+this.id+']');
if(ids.length>1 && ids[0]==this)
$(this).remove();
});
Fortunately what I was doing with the CAML query wasn't too complicated but if it were more complicated then I would have had to do something like iOnline247 suggested. Thanks again!

Apr 16, 2012 at 9:16 PM

kramden88,

Can you post an example of your entire GetListItems function?  This is something I'm trying to accomplish as well!


Thanks!

Charlie

Apr 17, 2012 at 1:03 PM
Edited Apr 17, 2012 at 1:07 PM

Hi Charlie,

This is a sample of a GetListItems function that I've used. You should also see Marc's documentation here.

$().SPServices({
     operation: "GetListItems",
    async: false, listName: "List Name",
    CAMLQuery: "<Query><Where><Eq><FieldRef Name='Column1' /><Value Type='Text'>Sample</Value></Eq></Where><OrderBy><FieldRef Name='ColumnX' Ascending='False' /></OrderBy></Query>",
    CAMLViewFields: "<ViewFields>"
        + "<FieldRef Name='ID' />"
        + "<FieldRef Name='Column1' />"
        + "<FieldRef Name='Column2' />"
        + "<FieldRef Name='Column3' />"
        + "</ViewFields>",
    completefunc: function (xData, Status) {
        // alert(xData.responseText);
        $(xData.responseXML).find("[nodeName='z:row']").each(function() {
            var ID = $(this).attr("ows_ID");
            var Column1 = $(this).attr("ows_Column1");
            var Column2 = $(this).attr("ows_Column2");
            var Column3 = $(this).attr("ows_Column3");

        $("tr#content").append('<td class="ms-vb2">'+Column1+'</td>\
                        <td class="ms-vb2">'+Column2+'</td>\
                        <td class="ms-vb2">'+Column3+'</td>\
                    </tr>');
        });//eachfunction
    }//completefunc
});//SPServices



You can also uncomment the alert(xData.responseText); line to see what the CAML query is returning.
Apr 17, 2012 at 1:46 PM

Hi,

if it's a big list that approach can be very slow. Then you can use the built in file filter.aspx that is better optimized for that. See example below

 

 

$.ajax({
	url: weburl+'/_layouts/filter.aspx?ListId='+escape(listId)+'&FieldInternalName='+internalName+'&ViewId='+escape(viewId)+'&FilterOnly=1&Filter=1',
	success: function(data) {
		//Do Something
	}
});

/Fredrik

 

Apr 17, 2012 at 7:04 PM

kramden88:

I do not see where your script is removing duplicates.  Can you point that out to me?  Sorry and thank you so much for your time!

Charlie

Apr 17, 2012 at 7:06 PM
Edited Apr 17, 2012 at 7:07 PM
fereko wrote:

Hi,

if it's a big list that approach can be very slow. Then you can use the built in file filter.aspx that is better optimized for that. See example below

 

 

$.ajax({
	url: weburl+'/_layouts/filter.aspx?ListId='+escape(listId)+'&FieldInternalName='+internalName+'&ViewId='+escape(viewId)+'&FilterOnly=1&Filter=1',
	success: function(data) {
		//Do Something
	}
});

/Fredrik

 


Fredrik:

Would I use this function within GetListItems?  Sorry, just trying to figure out how to capture the data I need from your suggestion.

Thanks!

Charlie

Apr 17, 2012 at 7:19 PM

Sorry, I thought you just wanted a sample of GetListItems. If it is returning duplicate values (which there isn't much you can do about) then what I've done is remove the duplicates after the fact. Unfortunately, it's a bit of a roundabout solution but I haven't come across a better way. What you would do is when you write the elements to the DOM you would put an id attribute for whatever tag you're using. If you look at the last step of the code I posted above then you would put an id attribute on the tr's like this:

$("tr#content").append('<td id="'+ID+'" class="ms-vb2">'+Column1+'</td>\
     <td class="ms-vb2">'+Column2+'</td>\
     <td class="ms-vb2">'+Column3+'</td>\
</tr>');

After the entire SPServices function you would use this code, which searches for duplicate id attributes and removes the tag and child tags associated with them:

$('[id]').each(function() {
     var
ids = $('[id='+this.id+']');
     if(ids.length>1 && ids[0]==this)
          $(this).remove();
})

Apr 17, 2012 at 9:09 PM

Charlie,

my suggestion above is not using GetListItems at all. Just try that code, change weburl to your site, listID to your listGUID, internalName to the column you want to use and viewId to the view GUID you want to look at. The function will then return (data) that is an empty web page with the unique items in a dropdown.

/F

Mar 10, 2015 at 8:26 AM
fereko hi,

I get this empty web page with unique items in a dropdown, but how can I now grab these values? Select has the ID specified and I tried to get values with $('#IDofselect option').each(function() ... but it does not work. Help? :)

BR, Sandra
Coordinator
Mar 10, 2015 at 8:35 PM
Sandra:

I'm not sure what you're up to, but this is a very old thread. If you're still having a problem, why don't you start a new thread with more details?

M.
Mar 11, 2015 at 1:41 PM
Thank you for your reply. I have started a new thread here.

BR, Sandra