Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
JShare
>
Tutorial
> Search by file extension and author
The following example shows you how to search by file extension and author.
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.TimeZone; import com.independentsoft.share.KeyValue; import com.independentsoft.share.SearchQuery; import com.independentsoft.share.SearchResult; import com.independentsoft.share.SearchResultPropertyName; import com.independentsoft.share.Service; import com.independentsoft.share.ServiceException; import com.independentsoft.share.SimpleDataRow; import com.independentsoft.share.SimpleDataTable; public class Example { public static void main(String[] args) { try { Service service = new Service("https://independentsoft.sharepoint.com", "username", "password"); com.independentsoft.share.kql.IsEqualTo restriction1 = new com.independentsoft.share.kql.IsEqualTo(SearchResultPropertyName.FILE_EXTENSION, "docx"); com.independentsoft.share.kql.IsEqualTo restriction2 = new com.independentsoft.share.kql.IsEqualTo(SearchResultPropertyName.AUTHOR, "Admin"); com.independentsoft.share.kql.And restriction3 = new com.independentsoft.share.kql.And(restriction1, restriction2); SearchQuery query = new SearchQuery(restriction3); query.getSelectProperties().add(SearchResultPropertyName.TITLE); query.getSelectProperties().add(SearchResultPropertyName.PATH); query.getSelectProperties().add(SearchResultPropertyName.CREATED_TIME); SearchResult searchResult = service.search(query); SimpleDataTable table = searchResult.getPrimaryQueryResult().getRelevantResult().getTable(); for (SimpleDataRow row : table.getRows()) { for (KeyValue cell : row.getCells()) { if (cell.getKey().equals(SearchResultPropertyName.TITLE)) { System.out.println("Title: " + cell.getValue()); } else if (cell.getKey().equals(SearchResultPropertyName.PATH)) { System.out.println("Path: " + cell.getValue()); } else if (cell.getKey().equals(SearchResultPropertyName.CREATED_TIME)) { Date localTime = toLocalTime(cell.getValue()); System.out.println("CreatedTime: " + localTime); } } } } catch (ServiceException ex) { System.out.println("Error: " + ex.getMessage()); System.out.println("Error: " + ex.getErrorCode()); System.out.println("Error: " + ex.getErrorString()); System.out.println("Error: " + ex.getRequestUrl()); ex.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } static Date toLocalTime(String utcTime) throws ParseException { if (utcTime != null && utcTime.length() > 0) { String format = "yyyy-MM-dd'T'HH:mm:ss.ssssss'Z'"; DateFormat dateFormat = new SimpleDateFormat(format, Locale.US); Calendar utcCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT")); dateFormat.setCalendar(utcCalendar); return dateFormat.parse(utcTime); } else { return null; } } }
Need help? Ask our developers:
Name*
Email*
Message*