Skip to content

CacheContactList does not work for > 5000 contact records. #2

@philmoz

Description

@philmoz

The code in CacheContactList (SessionManager.cs) is not setting the PagingCookie on the QueryExpression for the 2nd and subsequent queries when there are > 5000 contact records in CRM.

The following code works for me.

    public static void CacheContactList()
    {
        var moreRecords = true;
        var allContactsList = new List<Entity>();

        //query expression
        var qe = new QueryExpression(ContactLogicalName)
        {
            ColumnSet = new ColumnSet(CrmContactLookupFields.ToArray()),
            PageInfo = new PagingInfo
            {
                PageNumber = 1,
            }
        };

        while (moreRecords)
        {
            EntityCollection ents = null;
            Pool.Perform(xrm => { ents = xrm.RetrieveMultiple(qe); });
            allContactsList.AddRange(ents.Entities);
            moreRecords = ents.MoreRecords; //check to see if we have more records in the system

            if (moreRecords)
            {
                qe.PageInfo.PageNumber++;
                qe.PageInfo.PagingCookie = ents.PagingCookie;
            }
        }

        //replace the current cached list with the updated list
        //this ensures old contacts that were deleted are not included anymore
        lock (_cachedContacts)
        {
            _cachedContacts = allContactsList;
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions