0

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>
   <Where>
      <Eq>
         <FieldRef Name='Audience' />
         <Value Type='MultiChoice'>'+subsite'</Value>
      </Eq>
   </Where>
   <OrderBy>
      <FieldRef Name='Created' Ascending='False' />
   </OrderBy>
</Query>';
camlQuery.RowLimit = 3;
camlQuery.set_viewXml(query);
listItems = list.getItems(camlQuery);
context.load(listItems);
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 Apr 22 '15 at 15:49
  • Also, subsite.length will be a number, so it'll never equal 'SitePages' – wjervis Apr 22 '15 at 15:50
0

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

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

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, privacy policy and cookie policy

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