Use the sample code and data graphs to search by using the search() method, build search filters, and parse search results.
The following steps are covered in this sample code snippet:
Ensure that you have read the information and completed the steps described in the topic, Programming prerequisites.
Add the following sample code snippet to your application code and replace the variables with the actual values that you want to use.
/**
* This test builds a search filter, calls a search API,
* and parses the search results
*/
@SuppressWarnings("unchecked")
public static void testSearch() throws Exception
{
DataObject root = SDOHelper.createRootDataObject();
DataObject searchCtrl = SDOHelper.createControlDataObject(root, null, SchemaConstants.DO_SEARCH_CONTROL);
// Set the properties that need to be retrieved in search results
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn");
searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("telephoneNumber");
// Set the search base in which search will be performed
searchCtrl.getList(SchemaConstants.PROP_SEARCH_BASES).add("o=SalesPerson,dc=yourco,dc=com");
// Set the search filter as uid, here the expression will search for all uids
searchCtrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION, "@xsi:type='PersonAccount' and uid='*'");
System.out.println("Input datagraph before searching for users in the searchbase"+ printDO(root));
DataObject result = service.search(root);
System.out.println("Output datagraph after searching for users in the searchbase"+ printDO(result));
// Loop through the search results and print the uniqueName
printIdentifiers(result);
}
<?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>cn</wim:properties>
<wim:properties>sn</wim:properties>
<wim:properties>telephoneNumber</wim:properties>
<wim:searchBases>o=SalesPerson,dc=yourco,dc=com</wim:searchBases>
</wim:controls>
</wim:Root>
</sdo:datagraph>
<?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,o=SalesPerson,dc=yourco,dc=com"
repositoryId="ldapRepo" uniqueId="f3fe184d-e51a-4d02-a5c2-4e2e79e79fea"
uniqueName="uid=SalesPerson3,o=SalesPerson,dc=yourco,dc=com"/>
<wim:uid>SalesPerson3</wim:uid>
<wim:cn>SalesPerson3cn</wim:cn>
<wim:sn>SalesPerson3sn</wim:sn>
</wim:entities>
<wim:entities xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=SalesPerson1,o=SalesPerson,dc=yourco,dc=com"
repositoryId="ldapRepo" uniqueId="e0db3534-a0ec-4c8b-bfff-a399895a7ca3"
uniqueName="uid=SalesPerson1,o=SalesPerson,dc=yourco,dc=com"/>
<wim:uid>SalesPerson1</wim:uid>
<wim:cn>SalesPerson1cn</wim:cn>
<wim:sn>SalesPerson1sn</wim:sn>
<wim:telephoneNumber>11111111</wim:telephoneNumber>
</wim:entities>
<wim:entities xsi:type="wim:PersonAccount">
<wim:identifier externalName="uid=SalesPerson2,o=SalesPerson,dc=yourco,dc=com"
repositoryId="ldapRepo" uniqueId="7130a46b-d896-4bdd-8f2c-04d9374908c6"
uniqueName="uid=SalesPerson2,o=SalesPerson,dc=yourco,dc=com"/>
<wim:uid>SalesPerson2</wim:uid>
<wim:cn>SalesPerson2cn</wim:cn>
<wim:sn>SalesPerson2sn</wim:sn>
<wim:telephoneNumber>22222222</wim:telephoneNumber>
</wim:entities>
</wim:Root>
</sdo:datagraph>
UniqueName is -> uid=SalesPerson3,o=SalesPerson,dc=yourco,dc=com
UniqueName is -> uid=SalesPerson1,o=SalesPerson,dc=yourco,dc=com
UniqueName is -> uid=SalesPerson2,o=SalesPerson,dc=yourco,dc=com