0

I've got this code, which takes a part of the title to perform a query and filter part of the content of a list:

<script type="text/javascript">

var items_lista; 

    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', Initialize);

    function Initialize() {

        var PDP_Link_Filter = $("title").text().split("-")[1].split(".")[0];  

        PDP_Link_Filter = "Link_" + PDP_Link_Filter + "_"; 

        var contexto = SP.ClientContext.get_current();
        var web = contexto.get_web();
        var listas = web.get_lists();
        var parametros = listas.getByTitle('Parametrizacion_WF');

        var query = new SP.CamlQuery();

        query.set_viewXml("<Query><Where><Contains><FieldRef Name='Title'/>" + 
                          "<Value Type='Text'>" + PDP_Link_Filter + "</Value></Contains></Where></Query>");

        console.log(query);

        items_lista = parametros.getItems(query);
        contexto.load(items_lista);
        contexto.executeQueryAsync(Function.createDelegate(this, this.onRequestSucceeded), Function.createDelegate(this, this.onRequestFailed));

    } //Initialize 

    function onRequestSucceeded() 
    {
        console.log(items_lista); 
        for(i = 0; i < items_lista.get_count(); i++) 
        {
            console.log(items_lista.get_item(i).get_item('Title'));
        }       
    }
    function onRequestFailed() { console.log('Error'); }

</script>

The query filter that it generates (obtained through console.log()):

<Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>P000</Value></Contains></Where></Query>

But when the for loop runs it shows all the content of the list not just the rows that match the filter.

What am I doing wrong?

ADPX
  • 5
  • 5

1 Answers1

0

This is most probably related with malformed value for SP.CamlQuery.viewXml property. Since this property expects XML schema that defines the list view, the root element has to be View element, for example:

var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var items = list.getItems('<View Scope="RecursiveAll"><Query></Query></View>');

In your case enclose the query with View element:

<View>
   <Query>...</Query>
</View>
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • I enclosed it as you suggest, but now the query does not return any data. Am I doing correctly a like query? I wanna do a select of all items that have a title name like P000. I've tried even with '%P000%' but does not work either – ADPX Mar 03 '15 at 18:00
  • 1
    Sorry, my mistake. The query filter was wrong, I needed a trim(). With your solution works fine. Thank You! – ADPX Mar 03 '15 at 18:12