Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Looking to get monthly totals of items from a list using the SharePoint COM/SP Services. My intention is to pass this data to a D3 chart.

share|improve this question
    
Okay? What have you tried? Or are you just letting everyone know? –  thantos Apr 15 '13 at 16:06
    
I was looking for suggestions when I posted this and had not yet tried anything. Since posting it I came up with something that worked that I will post below. Perhaps there's a better way though? –  Dom Rocco Apr 15 '13 at 16:52
    
Generally you would edit your question if you are still questioning your approach, not as an answer. Also, the general pattern on SE is to come with code or errors before you ask for help. –  thantos Apr 15 '13 at 17:31
    
I'm here to solve a problem for work and am not at all concerned about how you like to have questions presented to you or your self-imposed police role. If you have nothing constructive to say, please go nitpick somewhere else and get a life. –  Dom Rocco Apr 15 '13 at 18:31
2  
I think @thantos is trying to help you form your question to increase the chance that members of the community will help you. Your subsequent comment is likely to have the opposite effect. [mod] –  SPDoctor Apr 16 '13 at 10:11
add comment

1 Answer 1

This works but maybe there's a better way... any suggestions?

function GetListItemsByMonth(year, listName, webUrl)
{
    var datarows = [];
    var yrStart = year + "-01-01:00:00:00";
    var yrEnd = year + "-12-31:23:59:59";
    var caml = "<Query><Where><And>";
    caml += "<Geq><FieldRef Name='Created' /><Value Type='DateTime'>" + yrStart + "</Value></Geq>";
    caml += "<Leq><FieldRef Name='Created' /><Value Type='DateTime'>" + yrEnd + "</Value></Leq>";
    caml += "</And></Where></Query>";
    $().SPServices({
        operation: "GetListItems",
        async: false,
        webURL: webUrl,
        listName: listName,
        CAMLQuery: caml,
        completefunc: function(xData,Status){
            $(xData.responseXML).SPFilterNode("z:row").each(function(){
                var d = new Date($(this).attr("ows_Created"));
                var x = {
                    month: d.getMonth()
                };
                datarows.push(x);
            });
        }
    });
    var counts = {},e;
    for (var i = 0, l=datarows.length; i < l; i++){
        e = datarows[i];
        counts[e.month] = (counts[e.month] || 0) + 1;
    }
    return counts;
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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