Search code examples
spservices

SPServices Get number of ListItems with the same titles


What i need is to find all list items with the same title using SPServices. I've made a CAML query from TextBox1 but have no idea what to do next. My question is: how do I change this code to accomplish my goal?

<script language="javascript" type="text/javascript">
    function GetTitleMatch() 
    {


        var Tit = $("#TextBox1").val();
       $().SPServices({
                     operation:"GetListItems",
                     listName:"CustomList",
                     async:false,
                     CAMLViewFields: "<ViewFields>"+
                                     "<FieldRef Name='Title'/>"+
                                     "<ViewFields>",
                     CAMLQuery:"<Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + Tit + "</Value></Eq></Where></Query>",
                       completefunc:function(xData,status)
                       {

                           alert($(xData.responseXML).find('[nodeName="z\\:row"]').length);
                       } 
                   });  
               }

    </script>
   <a href="#" onclick="javascript:GetTitleMatch();">click</a>

Solution

  • If anyone cares, the complete code should loock something like this:

    <script language="javascript" type="text/javascript">
    function GetTitleMatch() 
    {
    
    
        var Tit = $(".TextBox1").val();
        alert(Tit);
        var itemCount=0;
        var queryText = "<Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + Tit + "</Value></Eq></Where></Query>";
        alert(queryText);
        $().SPServices({
    
    
            operation: "GetListItems",
            listName: "CustomList",
            async: false,
    
            CAMLQuery: queryText,
    
            completefunc: function (xData, status) {
    
                alert(xData.responseXML.xml);
                itemCount = $(xData.responseXML.xml).find("rs\\:data, data").attr("ItemCount");
                alert(itemCount);
    
                $(".TextBox3").val(itemCount);
    
    
    
    
    
            }
        });
               }
    
    </script>
     <a onclick="javascript:GetTitleMatch();">click</a>
    

    All I neded to do was use .attr("ItemCount")