Performing a search
Use the search(DataObject) method to return a set of entities that match the search expression.
About this task
Note: In this example, the
paged and sorted search functions are not used.
To get the
uid and sn properties of all the person entities, add the following
sample code to your application code and replace the variables with
the actual values that you want to use for this search:
DataObject root = SDOHelper.createRootDataObject();
DataObject searchCtrl = SDOHelper.createControlDataObject(root, null, DO_SEARCH_CONTROL);
searchCtrl.getList(PROP_PROPERTIES).add("uid");
searchCtrl.getList(PROP_PROPERTIES).add("sn");
searchCtrl.getList(PROP_SEARCH_BASES).add("o=Sales,cn=users,dc=yourco,dc=com");
searchCtrl.setString(PROP_SEARCH_EXPRESSION, "@xsi:type='PersonAccount' and uid='*'");
root = service.search(root);
Note:
- If a property is applicable only to a particular repository and you want to include that property in a search expression, you must specify the searchbase in the search control to perform the search only in that repository. For example propInDB is only applicable to a database repository. If you wanted to search on propInDB='abc', you need to specify the searchbase for that database repository.
- If the actual search value contains a character such as an asterisk ( * ) that can also be used as a wild card, for example "hobby=abc*de", the character must be escaped using a backslash ( \ ), "hobby=abc\*de".
Sample input and output data graphs
For
this particular example the input data graph is:
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sdo="commonj.sdo"
xmlns:wim="http://www.ibm.com/websphere/wim">
<wim:Root>
<wim:controls xsi:type="wim:SearchControl"
expression="@xsi:type='PersonAccount' and uid='*'">
<wim:properties>uid</wim:properties>
<wim:properties>sn</wim:properties>
<wim:searchBases>o=Sales,cn=users,dc=yourco,dc=com</wim:searchBases>
</wim:controls>
</wim:Root>
</sdo:datagraph>
A sample output data graph for this example is:
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sdo="commonj.sdo"
xmlns:wim="http://www.ibm.com/websphere/wim">
<wim:Root>
<wim:entities xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=SalesPerson3,ou=US,o=Sales,cn=users,dc=yourco,dc=com"
repositoryId="LDAP1"
uniqueId="13ae346f-1e4c-40cd-8896-1c7f0b2b3079"
uniqueName="uid=SalesPerson3,ou=US,o=Sales,cn=users,dc=yourco,dc=com"/>
<wim:uid>SalesPerson3</wim:uid>
<wim:sn>SalesPerson3Sn</wim:sn>
</wim:entities>
<wim:entities xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=SalesPerson4,ou=US,o=Sales,cn=users,dc=yourco,dc=com"
repositoryId="LDAP1"
uniqueId="a2d1be42-212e-44f0-8396-0bfc2d7d346d"
uniqueName="uid=SalesPerson4,ou=US,o=Sales,cn=users,dc=yourco,dc=com"/>
<wim:uid>SalesPerson4</wim:uid>
<wim:sn>SalesPerson4Sn</wim:sn>
</wim:entities>
<wim:entities xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=SalesPerson2,ou=CA,o=Sales,cn=users,dc=yourco,dc=com"
repositoryId="LDAP1"
uniqueId="a3f9e8a1-117a-4252-840f-6d0cb079a7fb"
uniqueName="uid=SalesPerson2,ou=CA,o=Sales,cn=users,dc=yourco,dc=com"/>
<wim:uid>SalesPerson2</wim:uid>
<wim:sn>SalesPerson2Sn</wim:sn>
</wim:entities>
<wim:entities xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=SalesPerson1,ou=CA,o=Sales,cn=users,dc=yourco,dc=com"
repositoryId="LDAP1"
uniqueId="e7db8c0e-d2b3-43fe-be14-1475125606f4"
uniqueName="uid=SalesPerson1,ou=CA,o=Sales,cn=users,dc=yourco,dc=com"/>
<wim:uid>SalesPerson1</wim:uid>
<wim:sn>SalesPerson1Sn</wim:sn>
</wim:entities>
</wim:Root>
</sdo:datagraph>
Also see the topics, Sample code for performing a paged search with sorted results and Sample code for searching using search bases.