Guids in filters

9 posts, 0 answers
  1. John Darlington
    John Darlington avatar
    5 posts
    Member since:
    Nov 2009

    Posted 03 Apr 2012 Link to this post

    Our oData service is full of Guid as primary keys. So I need to be able to use guid data types in filters. Unfortunately when I use the following in the filter for the datasource:
         filter: { field: "UserID", operator: "eq", value: e.data.UserID }

     I get the following error from oData:
       "Operator 'eq' incompatible with operand types 'System.Guid' and 'System.String'
    
    Any ideas, The native url requires filter=UserID+eq+guid'ab4e630e-1c7f-49cb-919a-5e82786e753e' .. How can I adjust the filter command to acheive this.
    Thanks
  2. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 06 Apr 2012 Link to this post

    Hi,

     This can be done like this:

    dataSource: {
           type: "odata",
           serverFiltering: true,
           transport: {
               read: "<url to the service>",
               parameterMap: function(options) {
                   // call the default OData parameterMap
                   var result = kendo.data.transports.odata.parameterMap(options);
     
                   if (result.$filter) {
                      // encode everything which looks like a GUID
                      var guid = /('[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')/ig;
                      result.$filter = result.$filter.replace(guid, "guid$1");
                   }
                   return result;
               }
           }
       }

    All the best,
    Atanas Korchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
  4. Stevo
    Stevo avatar
    25 posts
    Member since:
    Jun 2013

    Posted 21 Nov 2014 in reply to Atanas Korchev Link to this post

    Hi,

    this works fine as long as strings don't look like guids. 

    Can you help us extending this functionality to cater for scenarios when some of the strings look like guid-s, but should be strings, and some of the strings are actually guid-s

    Thanks,
    Stevo
  5. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 25 Nov 2014 Link to this post

    Hello Stevo,

    I am sorry but I don't understand your requirements. Could you provide some test data? How are strings that look like guids different than strings that are guids?

    Regards,
    Atanas Korchev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  6. Stevo
    Stevo avatar
    25 posts
    Member since:
    Jun 2013

    Posted 01 Dec 2014 in reply to Atanas Korchev Link to this post

    Hi Atanas,

    my point is that the code you've provided will replace all filters that look like guids 'xxxx-xxx...' to a 'guid' filter condition (i.e. remove the apostrophes that are used for string filters)

    That is the desired behaviour if the .Net Property that you are filtering on is actually of type Guid. However if the property that you are filtering on is of type String, and it contains a string that looks like a guid, the filtering won't work, since the code will remove the apostrophes.

    Does that make sense? Just use whatever solution you have to filter on guid, than change your property type to string (make sure the values in there are guids.ToString()) and watch what happens.

    Hope that helps,
    Stevo
  7. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 02 Dec 2014 Link to this post

    Hello Stevo,

    The solution I have given is the only workaround possible. Kendo UI (and JavaScript) don't support a GUID type and this is why a regular expression is needed. Also guids are serialized in JSON as a string.
    What I can suggest is to change the regexp so it replaces only strings that fully match a guid (don't just contain a guid):

    /^('[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')$/ig;


    Regards,
    Atanas Korchev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  8. Stevo
    Stevo avatar
    25 posts
    Member since:
    Jun 2013

    Posted 02 Dec 2014 in reply to Atanas Korchev Link to this post

    Hi Atanas,

    exactly, kendoui does not cope with guids in OData. OData is a rich interface, getting more popular, having only 4 data types is not going to work very well. 

    One way of solving this is if the function kendo.data.transports.odata.parameterMap(options) would be more extensible (i.e. have the ability to override creating filter definition for each filter item), we could support additional data types (like guid) easily.

    Also parameterMap arguments don't contain references to the datasource schema model. If we had this, we could extend the schema model with our custom attributes and take them into consideration when creating say the $filter.

    Regards,
    Stevo



  9. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 02 Dec 2014 Link to this post

    Hello Stevo,

    Kendo UI doesn't currently support any of those - you can't override the filter creation and parameterMap doesn't have a reference to the data source model. You may consider opening a feature request in our feedback portal about supporting GUID filtering in OData.

    For now the suggestion workaround or creating a new custom transport are the only options possible. 

    Regards,
    Atanas Korchev
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  10. Casimodo
    Casimodo avatar
    71 posts
    Member since:
    Mar 2015

    Posted 01 Jul 2015 in reply to Atanas Korchev Link to this post

    The following is Atana's hack applied to OData V4:

    fixODataV4FilterParameterMap = function (data, type) {
        // This is needed for Kendo grid's filters when using OData v4.
      
        // Call the default OData V4 parameterMap.
        var result = kendo.data.transports["odata-v4"].parameterMap(data, type);
      
        if (result.$filter) {
      
            // Remove the single quotation marks around the GUID (OData v4).
            result.$filter = result.$filter.replace(/('[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')/ig, function (x) {              
                return x.substring(1, x.length - 1);
            });          
        }
        return result;
    };

Back to Top
Kendo UI is VS 2017 Ready