AlexaCRM / dynamics-webapi-toolkit

Dynamics 365 Web API Toolkit for PHP
MIT License
75 stars 58 forks source link

Ability to retrieve all records of a certain entity #28

Closed samihsoylu closed 4 years ago

samihsoylu commented 4 years ago

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

samihsoylu commented 4 years ago

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.

georged commented 4 years ago

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

samihsoylu commented 4 years ago

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;
}