简体   繁体   中英

How to do the following code in loop in sharepoint list

Here I have get the count from my sharepoint list when status is open and the created month is jan,feb,etc...The month field is a calculated column and it calculated the months of created field .....Here is my code

var month1 = '<View><Query><Where><And><Eq><FieldRef Name="Status"/><Value Type="Text">Open</Value></Eq><Eq><FieldRef Name="Monthname"/><Value Type="Text">Jan</Value></Eq></And></Where></Query></View>';
                        var month2 = '<View><Query><Where><And><Eq><FieldRef Name="Status"/><Value Type="Text">Open</Value></Eq><Eq><FieldRef Name="Monthname"/><Value Type="Text">Feb</Value></Eq></And></Where></Query></View>';
                        var month3 = '<View><Query><Where><And><Eq><FieldRef Name="Status"/><Value Type="Text">Open</Value></Eq><Eq><FieldRef Name="Monthname"/><Value Type="Text">Mar</Value></Eq></And></Where></Query></View>';
                        var month4 = '<View><Query><Where><And><Eq><FieldRef Name="Status"/><Value Type="Text">Open</Value></Eq><Eq><FieldRef Name="Monthname"/><Value Type="Text">Apr</Value></Eq></And></Where></Query></View>';
                        var month5 = '<View><Query><Where><And><Eq><FieldRef Name="Status"/><Value Type="Text">Open</Value></Eq><Eq><FieldRef Name="Monthname"/><Value Type="Text">May</Value></Eq></And></Where></Query></View>';
                        var month6 = '<View><Query><Where><And><Eq><FieldRef Name="Status"/><Value Type="Text">Open</Value></Eq><Eq><FieldRef Name="Monthname"/><Value Type="Text">Jun</Value></Eq></And></Where></Query></View>';
                        var month7 = '<View><Query><Where><And><Eq><FieldRef Name="Status"/><Value Type="Text">Open</Value></Eq><Eq><FieldRef Name="Monthname"/><Value Type="Text">Jul</Value></Eq></And></Where></Query></View>';
                        var month8 = '<View><Query><Where><And><Eq><FieldRef Name="Status"/><Value Type="Text">Open</Value></Eq><Eq><FieldRef Name="Monthname"/><Value Type="Text">Aug</Value></Eq></And></Where></Query></View>';
                        var month9 = '<View><Query><Where><And><Eq><FieldRef Name="Status"/><Value Type="Text">Open</Value></Eq><Eq><FieldRef Name="Monthname"/><Value Type="Text">Sep</Value></Eq></And></Where></Query></View>';
                        var month10 = '<View><Query><Where><And><Eq><FieldRef Name="Status"/><Value Type="Text">Open</Value></Eq><Eq><FieldRef Name="Monthname"/><Value Type="Text">Oct</Value></Eq></And></Where></Query></View>';
                        var month11 = '<View><Query><Where><And><Eq><FieldRef Name="Status"/><Value Type="Text">Open</Value></Eq><Eq><FieldRef Name="Monthname"/><Value Type="Text">Nov</Value></Eq></And></Where></Query></View>';
                        var month12 = '<View><Query><Where><And><Eq><FieldRef Name="Status"/><Value Type="Text">Open</Value></Eq><Eq><FieldRef Name="Monthname"/><Value Type="Text">Dec</Value></Eq></And></Where></Query></View>';

this contains the caml query for get the count for each months

                        var monthquery1 = new SP.CamlQuery();
                        monthquery1.set_viewXml(month1);
                        var monthitem1 = TicketList.getItems(monthquery1);

                        var monthquery2 = new SP.CamlQuery();
                        monthquery2.set_viewXml(month2);
                        var monthitem2 = TicketList.getItems(monthquery2);

                        var monthquery3 = new SP.CamlQuery();
                        monthquery3.set_viewXml(month3);
                        var monthitem3 = TicketList.getItems(monthquery3);

                        var monthquery4 = new SP.CamlQuery();
                        monthquery4.set_viewXml(month4);
                        var monthitem4 = TicketList.getItems(monthquery4);

                        var monthquery5 = new SP.CamlQuery();
                        monthquery5.set_viewXml(month5);
                        var monthitem5 = TicketList.getItems(monthquery5);

                        var monthquery6 = new SP.CamlQuery();
                        monthquery6.set_viewXml(month6);
                        var monthitem6 = TicketList.getItems(monthquery6);

                        var monthquery7 = new SP.CamlQuery();
                        monthquery7.set_viewXml(month7);
                        var monthitem7 = TicketList.getItems(monthquery7);

                        var monthquery8 = new SP.CamlQuery();
                        monthquery8.set_viewXml(month8);
                        var monthitem8 = TicketList.getItems(monthquery8);

                        var monthquery9 = new SP.CamlQuery();
                        monthquery9.set_viewXml(month9);
                        var monthitem9 = TicketList.getItems(monthquery9);

                        var monthquery10 = new SP.CamlQuery();
                        monthquery10.set_viewXml(month10);
                        var monthitem10 = TicketList.getItems(monthquery10);

                        var monthquery11 = new SP.CamlQuery();
                        monthquery11.set_viewXml(month11);
                        var monthitem11 = TicketList.getItems(monthquery11);

                        var monthquery12 = new SP.CamlQuery();
                        monthquery12.set_viewXml(month12);
                        var monthitem12 = TicketList.getItems(monthquery12);




                        context.load(monthitem1);
                        context.load(monthitem2);
                        context.load(monthitem3);
                        context.load(monthitem4);
                        context.load(monthitem5);
                        context.load(monthitem6);
                        context.load(monthitem7);
                        context.load(monthitem8);
                        context.load(monthitem9);
                        context.load(monthitem10);
                        context.load(monthitem11);
                        context.load(monthitem12);

and here i will load the data to my list .....

                        context.executeQueryAsync(
                            function () {
                                var monthid1 = monthitem1.get_count();
                                var monthid2 = monthitem2.get_count();
                                var monthid3 = monthitem3.get_count();
                                var monthid4 = monthitem4.get_count();
                                var monthid5 = monthitem5.get_count();
                                var monthid6 = monthitem6.get_count();
                                var monthid7 = monthitem7.get_count();
                                var monthid8 = monthitem8.get_count();
                                var monthid9 = monthitem9.get_count();
                                var monthid10 = monthitem10.get_count();
                                var monthid11 = monthitem11.get_count();
                                var monthid12 = monthitem12.get_count();
                                      });

And this success function to get the count of my caml query..

Now what is problem is i don't know how to do this in loop function.. how can i achieve this in a while loop please help me

or Is there any other ways to get the count for each months??

We can use REST API with jQuery Ajax to achieve it.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {         
    var month=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
    for(var i=0;i<month.length;i++){
        var monthCount=getItemCountByFilter("$filter=Status eq 'Open' and Monthname eq '"+month[i]+"'");
        $("#allData").append("<p>"+month[i]+":"+monthCount+"</p>");
    }
});
function getItemCountByFilter(filter){
    var count=0;
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('TaskList')/items?$top=5000&$select=ID&"+filter,
        type: "GET",
        async:false,
        headers: {
            "Accept": "application/json;odata=verbose",
        },
        success: function (data) {
            count=data.d.results.length;
        },
        error: function (error) {
            console.log(JSON.stringify(error));
        }
    });
    return count;
}
</script>
<div id="allData"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM