Microsoft Graph API Sharepoint Search

Copper Contributor

I have written an some python to send a request to the graph API to search a OneDrive for files containing keywords.

def get_results(keywords, drive_id, next_url=None, total_results=[]):
    search_query = ' AND '.join(keywords)
    top = 500  # Set the desired number of results to retrieve
    select_properties = "id,name,createdDateTime"  # Example properties to select

    if not next_url:
        base_search_url = f"https://graph.microsoft.com/v1.0/drives/{drive_id}/root/search(q=\'{search_query}\')"
        top_search = f"$top={top}"
        search_url = f"{base_search_url}?{top_search}"               
    else:
        search_url = next_url

    headers = {
        'Authorization': 'Bearer {}'.format(access_token),
        'Content-Type': 'application/json'
    }

    response = requests.get(search_url, headers=headers)
    search_results = response.json()
    total_results += search_results.get('value', [])

    if '@odata.nextLink' in search_results:
        next_url = search_results['@odata.nextLink']
        get_results(keywords, drive_id, next_url, total_results)

    return total_results


However, when I run this, the result is that I get around 7k results, but I know for a fact there should be around 10k results. When I search the files in the OneDrive using body: keyword1 AND keyword2 I get all files returned, but with this method using the skip tokens, some files are not returned.

On top of this, I also get duplicates, so if the 7k files, only 6k are unique.

I'm not sure what I've done wrong with this method, I'm wondering if I could get some assistance on this. 

Thanks :)

 

0 Replies