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 Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Carolyne-3676 871 Reputation points
    2025-02-10T07:16:40.66+00:00

    At the very least in terms of troubleshooting, ensure that your application has the necessary permissions to access the DriveItem E.g Files.Read.All or Files.ReadWrite.All. You might be using different accounts/permissions in Graph Explorer and your Java application. Ensure that the SDK is also up to date. From your code, there are some issues you need to fix. Path should be enclosed in double quotes. Could you try this

    String encodedPath = filePath.replace(" ", "+");
    String queryString= "path: \\"" + filePath + "\\";
    query.setQueryString(queryString);
    
    0 comments No comments

Your answer

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