I have a caml query that I'm concatenating with a javascript variable of type string. It queries the column names 'Audience' and returns the first 3 ordered by the newest.

The variable subsite gets the current subsite unless there is none and therefore is set to the 'Home Page' audience. (An option in the column) Is there a way to do this in JS?

var subsite = window.location.pathname.split('/')[1];
if(subsite.length == 'SitePages')
    subsite = 'Home Page';

var camlQuery = new SP.CamlQuery();
var query = '<Query>
         <FieldRef Name='Audience' />
         <Value Type='MultiChoice'>'+subsite'</Value>
      <FieldRef Name='Created' Ascending='False' />
camlQuery.RowLimit = 3;
listItems = list.getItems(camlQuery);
context.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded),
    Function.createDelegate(this, onQueryFailed));

function onQuerySucceeded(sender, args){
  • Is this the exact code you are using? I can see a few syntax errors. It looks like you're getting the current subsite from the url, why aren't you simply using ctx.get_web().get_title()? It's more reliable than the URL
    – wjervis
    Commented Apr 22, 2015 at 15:49
  • Also, subsite.length will be a number, so it'll never equal 'SitePages'
    – wjervis
    Commented Apr 22, 2015 at 15:50

1 Answer 1


While there appear to be some other issues with the code you could concat your string like so:

var query = "<Query>" +
                "<Where>" +
                        "<FieldRef Name='Audience' />"+
                        "<Value Type='MultiChoice'>"+subsite+"</Value>"+
                "</Where>" +
                    "<FieldRef Name='Created' Ascending='False' />"+

If you don't mind bunching up some of the caml query this could be greatly reduced.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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