MS Graph Search/Query calls missing all my fields

274 Views Asked by At

I've been testing / playing around with the following code for the past few days now and it used to return all the fields I explicitly request. But now I only get back title, author, and url. All the "ids" are missing.
I haven’t changed anything as far as I recall - famous last words, I know.

Here's what I think is the relevant code:

            var graphClient = new GraphServiceClient(clientSecretCredential, scopes, "https://graph.microsoft.com/v1.0");

            var requestBody = new Microsoft.Graph.Search.Query.QueryPostRequestBody
            {
                Requests = new List<SearchRequest>
                    {
                        new SearchRequest
                        {
                            EntityTypes = new List<EntityType?>
                            {
                                EntityType.DriveItem,
                            },
                            Query = new SearchQuery
                            {
                                QueryString = this.searchQuery,
                            },

                            Fields = new List<string>
                            {
                                "itemId",
                                "author",
                                "title",
                                "url",
                                "rank"
                            },
                            Size=numberOfSearchResults, 
                            QueryAlterationOptions = new SearchAlterationOptions
                            {
                                EnableSuggestion = true,
                                EnableModification = true,
                            }
                           
                            , Region ="US"
                        },
                    },
            };

            var searchResults = await graphClient.Search.Query.PostAsync(requestBody).ConfigureAwait(false);

            if (searchResults.Value.Count > 0 && searchResults.Value[0].HitsContainers.Count > 0 && searchResults.Value[0].HitsContainers[0].Hits.Count > 0)
            {
                //this method just adds everything to a List to return to front end.  If a field is missing, I substitute the null value with "Unknown <fieldname>". 

                jsonResults = ExtractDriveItemDetails(searchResults);
            }
            return jsonResults;
        }

This is what the ExtractDriveItemDetails() looks like in part (i do the same thing for each field basically)

      foreach (var hitContainer in searchResults.Value.SelectMany(result => result.HitsContainers))
            {
                foreach (var hit in hitContainer.Hits)
                {
                    // Check if the resource is a DriveItem
                    if (hit.Resource is DriveItem driveItem)
                    {
                        var driveItemDetails = new Dictionary<string, string?>();

                        // Get the AdditionalData dictionary from the driveItem
                        var additionalData = driveItem.ListItem.Fields.AdditionalData;

                        // Use TryGetValue to safely access the values and handle null or missing keys
                        string? driveId = "Unknown Drive Id";
                        if (driveItem.ParentReference?.AdditionalData.TryGetValue("driveId", out var driveIdValue) == true && driveIdValue is string driveIdString)
                        {
                            driveId = driveIdString;
                        }

                        string? siteId = "Unknown Site Id";
                        if (additionalData.TryGetValue("siteId", out var siteIdValue) && siteIdValue is string siteIdString)
                        {
                            siteId = siteIdString;
                        }

                        driveItemDetails.Add("driveId", driveId);
                        driveItemDetails.Add("siteId", siteId);

Hopefully it’s something super simple that I’ve messed up.

In case it helps, here's a screenshot of the searchResults variable before I try to parse it:

enter image description here

I'm going to add some additional permissions to the application registration I have. But it already had / has:

Files.Read
Sites.Read.All
Files.Read.All
Sites.ReadWrite.All

Eventhough the queries worked yesterday with the permissions as is, just in case, I'm now going to try to add:

 Sites.Selected
0

There are 0 best solutions below