CMDBuild Forum

Wildcards search and filter with REST API

Dear Tecnoteca or Developer,

I'm trying to make wilcards searchs and filters through the REST API and using jQuery library.

I found some wilcards searchs and filters example on the forum and on the web but none worked so I decided to start a new discussion.

Here is one example of each that I tried  :

 

wildcards search :

$.ajax({
            type: "GET",
            url: url + 'cql?filter={CQL: "from Datasheet where Description CONTAINS bla"}',
            contentType : "application/json",
            cache : false,
            dataType: "json",
            crossDomain: true,
            headers: head
        }).done(function (data) {
              alert('done!');
        }).fail(function(XMLHttpRequest, textStatus, errorThrown) {
            // failure callback
        }).always(function() {
            // callback allways called
        });
};

 

filter :

function search(){
    var filter = '?filter:{"attribute":{"simple":{"attribute":"Code","operator":"contain","value":["Data"]}}}';
    $.ajax({
        type: "GET",
        url: url + 'classes/Datasheet/cards' + filter,
        cache : false,
        dataType: "json",
        crossDomain: true,
        contentType: 'application/json',
        headers: head,
    }).done(function (data) {
          alert('done!');
    }).fail(function(XMLHttpRequest, textStatus, errorThrown) {
        // failure callback
    }).always(function() {
        // callback allways called
    });
};

 

The header contains the sessionid like this : head["CMDBuild-Authorization"] = tocken;

And simple search are working perfectly so I assume that it's just the way I'm passing the filter and the CQL elements that's not working.

Thanks in advance,

Peter

 

The best way to send data to the server with jQuery.ajax is to add data property. For more details refer to jQuery ajax documentation
 
The value of the filter property must be a string. Here an example.
 
function search(){
    var attr_filter = {filter: '{"attribute":{"simple":{"attribute":"Code","operator":"contain","value":["Data"]}}}'};
    var cql_filter = {filter: '{"CQL":"from Datasheet ..."}'};
    $.ajax({
        type: "GET",
        url: url,  //http://mycmdbuild/services/rest/v2/classes/Datasheet/cards
        data: attr_filter, //or cql_filter
        cache : false,
        dataType: "json",
        crossDomain: true,
        contentType: 'application/json',
        headers: head,
    }).done(function (data) {
          alert('done!');
    }).fail(function(XMLHttpRequest, textStatus, errorThrown) {
        // failure callback
    }).always(function() {
        // callback allways called
    });
};
 
CMDBuild Team
 
 
Previously Peter wrote:

Dear Tecnoteca or Developer,

I'm trying to make wilcards searchs and filters through the REST API and using jQuery library.
I found some wilcards searchs and filters example on the forum and on the web but none worked so I decided to start a new discussion.
Here is one example of each that I tried  :

wildcards search :

$.ajax({
            type: "GET",
            url: url + 'cql?filter={CQL: "from Datasheet where Description CONTAINS bla"}',
            contentType : "application/json",
            cache : false,
            dataType: "json",
            crossDomain: true,
            headers: head
        }).done(function (data) {
              alert('done!');
        }).fail(function(XMLHttpRequest, textStatus, errorThrown) {
            // failure callback
        }).always(function() {
            // callback allways called
        });
};

filter :

function search(){
    var filter = '?filter:{"attribute":{"simple":{"attribute":"Code","operator":"contain","value":["Data"]}}}';
    $.ajax({
        type: "GET",
        url: url + 'classes/Datasheet/cards' + filter,
        cache : false,
        dataType: "json",
        crossDomain: true,
        contentType: 'application/json',
        headers: head,
    }).done(function (data) {
          alert('done!');
    }).fail(function(XMLHttpRequest, textStatus, errorThrown) {
        // failure callback
    }).always(function() {
        // callback allways called
    });
};

The header contains the sessionid like this : head["CMDBuild-Authorization"] = tocken;

And simple search are working perfectly so I assume that it's just the way I'm passing the filter and the CQL elements that's not working.

Thanks in advance,

Peter

That's working perfectly well.

 

I had to search a bit to find the right CQL query so here is a working example for any interested reader :

 

function search2(){

       var cql_filter = {filter: '{"CQL":"from Datasheet WHERE Description CONTAINS \'Datasheet2\'"}'};

        $.ajax({

        type: "GET",

        url: url + 'cql',  //http://mycmdbuild/services/rest/v2/classes/Datasheet/cards

        data: cql_filter, //or attr_filter

        cache : false,

        dataType: "json",

        crossDomain: true,

        contentType: 'application/json',

        headers: head,

    }).done(function (data) {

          alert('done!');

    }).fail(function(XMLHttpRequest, textStatus, errorThrown) {

        // failure callback

    }).always(function() {

        // callback allways called

    });

};

 

Thank you for your quick and accurate answer!

 

Peterd