Using FilterXML parameter in SmartObject REST Services

When you are using K2 SmartObject REST services then you would have observed a query parameter with name FilterXML for all the list methods. This parameter can be used to filter out the data while executing the list method of SmartObject. But using this parameter is a kind of tricky for beginners unless you follow this simple trick which I’m going to explain below.

Let’s see a sample SmartObject (Students.SMO) which returns list of students as below.

1SMO.jpg

Now the SmartObject Service’s REST endpoint URL for above SMO will be as below

https://k2-server:8443/SmartObjectServices/rest/Vijay/SmartObjects/Students.SMO/Get List?$format={format}&$top={top}&$skip={skip}&filterXml={filterXml}&RollNumber={RollNumber}

As RollNumber is the id column of Students.SMO  smartobject, By default REST URL is giving us the RollNumber as a URIParameter

Now if you want to use other columns as a filter to get data, we have no other option than using filterXML.

As the parameter says it accepts XML based filters.

Now let’s suppose there is scenario where you want get the list of students where percentage is greater than 90. Now the filterXML for this would be like below

Condition: Percentage > 90

<filterexp>
 <greaterthan>
   <left>
     <propertyexp name="Percentage" sotype="Text" />
   </left>
   <right>
     <valueexp sotype="text">90</valueexp>
   </right>
 </greaterthan>
 </filterexp>

 

Condition: Percentage >= 90

<filterexp>
 <or>
   <left>
     <greaterthan>
       <left>
         <propertyexp name="Percentage" sotype="Text" />
       </left>
       <right>
         <valueexp sotype="text">90</valueexp>
       </right>
     </greaterthan>
   </left>
   <right>
    <equals>
      <left>
        <propertyexp name="Percentage" sotype="Text" />
      </left>
      <right>
        <valueexp sotype="text">90</valueexp>
      </right>
    </equals>
  </right>
</or>
</filterexp>

 Now we just have to pass this as a value to URIParameter filterXML

https://k2-server:8443/SmartObjectServices/rest/Vijay/SmartObjects/Students.SMO/Get List?$format=JSON&filterXml=<filterexp><greaterthan><left><propertyexp name=”Percentage” sotype=”Text” /></left><right><valueexp sotype=”text”>90</valueexp></right></greaterthan></filterexp>

Now the simpler way to create these filterXML values is using SmartObjects – Services-Tester utility tool. Select Execute Smartobject and click Set button beside filter option which is shown highlighted in below screen shot

2Filter

Now in below screen we need to select the column that we want to use as filter in Left Property (here it is Percentage) and in Logical Filter select the logical operator (here it is GreaterThan) and Right Property you will have an option to select a column or directly provide a value (here in my case it is 90) and click add to see the xml representation of filter. See below screen shot

3Filter

Now copy the xml from the bottom window, remove the first line (<?xml version=”1.0″ encoding=”utf-16″?>) and then you need to add <filterexp> and </filterexp> at the beginning and end of the copied xml respectively. Also remove the line breaks and spaces in the copied filterXML and then it ready for use.

Copy paste the url in chrome and hit enter button to see the data getting filtered as mentioned in filterXML

4Result.jpg

Advertisements

Author: Vijay

I'm a K2 Certified Developer, helping teams to automate, design, develop & maintain business process applications connecting across multiple platforms with multiple technologies.

3 thoughts on “Using FilterXML parameter in SmartObject REST Services”

  1. Hi Vijay, I am impressed by then way you have displayed the solution above in a simplified way. Could you help me solving one query. Using above Rest url, I need to get data using sort . you can reply me on my email satendramishra91@gmail.com

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s