Graph Search Return no values when search in java

Felix Read 30 Reputation points
2024-11-19T00:18:56.0366667+00:00

I am doing a search in graph explorer for a file using the path ,

when i do the search in graph explorer the file is found,

but when i do the same search in the java api i dont seem to be able to find the file.

here is part of my java code

LinkedList<SearchRequest> requestsList = new LinkedList<>();
SearchRequest requests = new SearchRequest();
LinkedList<EntityType> entityTypesList = new LinkedList<>();
entityTypesList.add(EntityType.DriveItem);
requests.setEntityTypes(entityTypesList);
LinkedList<SortProperty> sortProperties = new LinkedList<>();
SortProperty sort = new SortProperty();
sort.setName("lastModifiedDateTime");
sort.setIsDescending(true);
sortProperties.add(sort);
logger.info(uuid+" Generating Search Query for source file : Path:\""+filePath+"\"");
SearchQuery query = new SearchQuery();
String encodedPath = encodedPath = filePath.replace(" ", "+");
String queryString= "Path:\""+filePath+"\"";
query.setQueryString(queryString);
requests.setQuery(query);
requests.setSortProperties(sortProperties);
requests.setSize(1);
requests.setRegion("US");
requestsList.add(requests);
QueryPostRequestBody qprb = new QueryPostRequestBody();
qprb.setRequests(requestsList);
DriveItem fileResult = null;
String resultMessage = null;
try {
	logger.info(uuid+" Doing Search :");
	QueryPostResponse searchResult = graphClient.search().query().post(qprb);
	for (SearchResponse searchResponse : searchResult.getValue()) {
		if (searchResponse.getHitsContainers() != null) {
			for (SearchHitsContainer searchHitsContainer : searchResponse.getHitsContainers()) {
				logger.info(uuid + " Search Response Size 1" + searchResponse.getHitsContainers().size());
				if (searchHitsContainer.getHits() != null) {
					logger.info(uuid + " Search Response Size 2" + searchResponse.getHitsContainers().size());
					for (SearchHit searchHit : searchHitsContainer.getHits()) {
					fileResult = (DriveItem) searchHit.getResource();
					logger.info(uuid + " File Found with ID: " + fileResult.getId() + " and path: '" + fileResult.getWebUrl() + "' ");
					}
				}
			}
		}
	}
}catch (Throwable ex){
	resultMessage = uuid + " Error Doing Search for file "+filePath+" "+ex.getMessage();
	sendAlertError(resultMessage);
}

when i do the search for this specific path,

https://tennat.sharepoint.com/sites/Test-Site/Shared Documents/Coverage Companies/IsBank/IS Bank_Model v3.0 - 53.xlsx  

in graph explorer i find the file but in java not.

Any ideas or tips on how to do this.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,467 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.