4

My requirement is to get the unique values from the column "Month" I am using the below code to group the column "Month" , But I don't find any luck .Can any one tell me what's wrong in my below code with JSOM.

camlQuery.set_viewXml("<View><Query><GroupBy collapse='true'><FieldRef Name='Month' /></GroupBy><OrderBy><FieldRef Name='Month' Ascending='TRUE' /></OrderBy></Query></View>");

1 Answer 1

2

Afaik you can't use group by like that, you'll have to use the renderListData function, and you need an aggration type.

Example:

SP.SOD.executeFunc("sp.js", "SP.ClientContext", () => { 
    var ctx = SP.ClientContext.get_current();
    var viewXml = "<View><Aggregations Value='On'><FieldRef Name='Month' Type='COUNT'/></Aggregations><Query><GroupBy Collapse='TRUE' GroupLimit='100'><FieldRef Name='Month' /></GroupBy><OrderBy><FieldRef Name='Month' Ascending='TRUE' /></OrderBy></Query></View>";
    var result = ctx.get_web().get_lists().getByTitle('ListTitleHere').renderListData(viewXml);
    ctx.executeQueryAsync(() => {
        console.log("res", JSON.parse( result.get_value()) ); 
    },(s,a) => {
        console.log("err",a.get_message());
    });
});

Edit:

To get from a specific folder you can add a <Where /> to the <Query />

Example viewXml:

var viewXml = "<View Scope='RecursiveAll'><Aggregations Value='On'><FieldRef Name='Month' Type='COUNT'/></Aggregations><Query><Where><Eq><FieldRef Name='FileDirRef' /><Value Type='Text'>"+_spPageContextInfo.webServerRelativeUrl+"/Shared Documents/FolderA</Value></Eq></Where><GroupBy Collapse='TRUE' GroupLimit='100'><FieldRef Name='Month' /></GroupBy><OrderBy><FieldRef Name='Month' Ascending='TRUE' /></OrderBy></Query></View>";
6
  • can you tell me how to run that against a folder in the list? May 4, 2018 at 12:55
  • @RichardHousham updated my answer May 4, 2018 at 13:21
  • Thanks for that however that's digging through all the folders/files on the site. And we have a big site. So I'm concerned with performance. Any other ideas?. I've been playing around with this stuff for a few days and when I do it I get duplicates. sharepoint.stackexchange.com/questions/240764/… Can you assist? I'm thinking it might be a 2013 issue and could be resolved in 2016 which we are set to move to. Should I just hold out? May 4, 2018 at 13:45
  • Well I gave your idea a go but no dice. Still getting duplicates. FYI the RecursiveAll gives you back folders as well. No matter what the filter on the folder says. stackoverflow.com/questions/333328/… May 4, 2018 at 15:05
  • @RichardHousham yes ofc, you'll have to add FSObjType eq 0 to your where query to exlude folders if you don't want them May 7, 2018 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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