AlexaCRM/dynamics-webapi-toolkit

Ability to retrieve all records of a certain entity

samihsoylu opened this issue · 3 comments

Is it currently possible to retrieve all records in an existing property? I have had a look inside the docs but I could not find anything about this. It would be nice if this is built in, such as a "findAll" function in the client

Nevermind, the pagination part indeed covers this, and also if you QueryByAttribute and provide no attributes to filter by, you retrieve all records. This is of course not very practical for growing entities, but for ones that don't, it works like a charm.

@samihsoylu I believe you'll still hit 5000 page limit. Are you saying QueryByAttribute returns more than that?

No it does not, I am only working with 20-40 records. Nevertheless I actually followed the documentation in the end to use a while loop.

Example on how to find all records in an entity:

public function findAll(string $entityName, array $columnNames = null, string $sortByColumnName = null): array
{
    $pagingInfo = new PagingInfo();
    $pagingInfo->Count = 10;

    $query = new QueryByAttribute($entityName);
    $query->ColumnSet = new ColumnSet($columnNames ?? true);
    if ($sortByColumnName !== null) {
        $query->AddOrder($sortByColumnName, OrderType::Ascending());
    }
    $query->PageInfo = $pagingInfo;

    $result = $this->client->RetrieveMultiple($query);

    $response = [];
    foreach ($result->Entities as $entity) {
        $response[] = $entity;
    }
    while ($result->MoreRecords) {
        $pagingInfo->PagingCookie = $result->PagingCookie;
        $result = $this->client->RetrieveMultiple($query);
        foreach ($result->Entities as $entity) {
            $response[] = $entity;
        }
    }

    return $response;
}