mnatan.brito
mnatan.brito

Reputation: 883

Caml builder query doesn't work in code

i'm in trouble here! I have a sharepoint site in which there is a photo gallery list (PhotoGallery). I need to query this list do bring back the images from a specified gallery(folder). I developed a web part with a select menu which loads all the galeries(folders) inside this photo gallery. Then, when i select a specific gallery(folder), a modal shows up with the images from that gallery(folder). But i'm not able to bring the right results back. The query returns nothing but the list of all galleries(folders) of the list. No images are returned. But the funny thing about this is that when i stop the browser and copy the query already with the folder value and paste it into Caml Query Helper, the query works just fine! My query is:

var camlQ = new SP.CamlQuery();

camlQ.set_viewXml("<Where><And><Eq><FieldRef Name=\"ContentType\" /><Value Type=\"Text\">Image</Value></Eq><Contains><FieldRef Name=\"FileRef\" /><Value Type=\"Text\">" + VALUE_FROM_OPTION + "</Value></Contains></And></Where>");

var clientCtx = new SP.ClientContext.get_current();
this.results = clientCtx.get_web().get_lists().getByTitle("PHOTO_GALLERY_NAME").getItems(camlQ);

clientCtx.load(this.results);

clientCtx.executeQueryAsync(Function.createDelegate(this, function(sender, args){

    // SOME CODE

}), Function.createDelegate(this, function(sender, args){

    // SOME CODE

}));

Upvotes: 0

Views: 971

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

Your CAML query seems to be correct but SP.CamlQuery.set_view method accepts xml where root element is View (Query element is also required).

So, wrap your Where block in a Query element and then wrap it in a View element, for example:

var query=new SP.CamlQuery();
query.set_viewXml('<View Scope="RecursiveAll"><Query><Where><Eq><FieldRef Name="FSObjType" /><Value Type="Integer">1</Value></Eq></Where></Query></View>');

Upvotes: 1

Related Questions