Contacts.Request Not getting all contacts

Meir Rotfleisch 161 Reputation points
2022-05-15T08:33:37.487+00:00

Hi

When i do a request for contacts I only get about 10 contacts returned? Suggestions ?

Regards

Meir

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

Accepted answer
  1. Zehui Yao_MSFT 5,831 Reputation points
    2022-05-17T08:06:22.373+00:00

    Hello @Meir Rotfleisch , The PageIterator class simplifies consuming of paged collections. PageIterator handles enumerating the current page and requesting subsequent pages automatically. Here is the documentation for your reference :https://learn.microsoft.com/en-us/graph/sdks/paging?tabs=csharp

    var messages = await graphClient.Me.Messages  
        .Request()  
        .Header("Prefer", "outlook.body-content-type=\"text\"")  
        .Select(e => new {  
            e.Sender,  
            e.Subject,  
            e.Body  
        })  
        .Top(10)  
        .GetAsync();  
      
    var pageIterator = PageIterator<Message>  
        .CreatePageIterator(  
            graphClient,  
            messages,  
            // Callback executed for each item in  
            // the collection  
            (m) =>  
            {  
                Console.WriteLine(m.Subject);  
                return true;  
            },  
            // Used to configure subsequent page  
            // requests  
            (req) =>  
            {  
                // Re-add the header to subsequent requests  
                req.Header("Prefer", "outlook.body-content-type=\"text\"");  
                return req;  
            }  
        );  
      
    await pageIterator.IterateAsync();  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. HarmeetSingh7172 4,811 Reputation points
    2022-05-16T10:25:51.82+00:00

    Hi @Meir Rotfleisch

    Using Microsoft graph API, you don't get all the results on a single page (in API response). You get an "@odata.nextLink" in response, using which you can retrieve next set of results.

    In order to check/count the number of contacts, I have used me/contacts?$count=true (refer to the attached screenshot) and as the total count is 13 (in API response), so I am getting a "@odata.nextLink" in response. Now using this link, I can get the next set of results.

    Please refer below screenshot for better understanding.

    202303-contacts.png

    Hope this helps.
    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have further questions about this answer, please click "Comment".

    1 person found this answer helpful.

  2. Meir Rotfleisch 161 Reputation points
    2022-05-16T09:41:20.567+00:00

    HI

    Seems the default for the .GetAsync()_ is 10 Records

    What needs to be added is to send to the GraphHelper function an integer as such

    public static async Task<IEnumerable<Microsoft.Graph.Contact>> GetContactsAsync(int P)
    {
    var graphClient = GetAuthenticatedClient();

            var events = await graphClient.Me.Contacts.Request()
            .Skip(P)
                .GetAsync();
    
            return events.CurrentPage;
        }
    

    And in the Controller you need to keep track and send each time a value for the P :) to get the paging

    Regards

    Meir

    0 comments No comments