Search code examples
dynamics-crmxrm

Is Dynamics CRM 2013 sdk cache result of QueryExpresions by Default?


I wrote this simple query:

var connectionString = String.Format("Url={0}; Username={1}; Password={2}; Domain={3}", url, username, password, domain);
var myConnection = CrmConnection.Parse(connectionString);
CrmOrganizationServiceContext _service = new CrmOrganizationServiceContext(myConnection);
var whoAmI = _service.Execute(new WhoAmIRequest());

var query = new QueryExpression
        {
            EntityName = "phonecall",
            ColumnSet = new ColumnSet(true)
        };

        query.PageInfo = new PagingInfo
        {
            Count = 20,
            PageNumber = 1,
            PagingCookie = null
        };

        query.Orders.Add(new OrderExpression
        {
            AttributeName = "actualstart",
            OrderType = OrderType.Descending
        });

        query.Criteria = new FilterExpression() { FilterOperator = LogicalOperator.And };
        query.Criteria.AddCondition("call_caller", ConditionOperator.In, lines);

var entities = _service.RetrieveMultiple(query).Entities;

I have a program which runs this query every minute. On the first execution the correct results are displayed but for subsequent queries the results never change as I update records in CRM.

If I restart my program the results refresh correctly again on the first load.

Why are the results not updating as records are modified in CRM?


Solution

  • It is the CrmOrganizationServiceContext that is doing the caching - I found the following worked a treat and the results of my RetrieveMultiple are no longer cached :)

    Context = new CrmOrganizationServiceContext(CrmConnection.Parse(connectionString));
    Context.TryAccessCache(cache => cache.Mode = OrganizationServiceCacheMode.Disabled);