Search code examples
c#soapcrmxrm

CRM 2013: Getting Data from Organization Service with Microsoft.Xrm


I am trying to get data using my Organization.svc and C#

This code successfully gets the data the first time it is run. However, when I call it again, it gets called again, the data pulls nearly instantaneously.

I disabled my internet connection, and found that it was still getting data.

I'm not sure if my data is being cached locally, but it certainly is not coming from my CRM server.

What can I do to get the actual data after the first call?

    // CONSTANTS DEFINED UP HERE 
    // (SERVICE_URI, USERNAME, PASSWORD, TIME_ENTITY_NAME,GUID, TIME_QTY)
    //

    static void Main(string[] args)
    {
        while (true)
        {
            CrmConnection connection = new CrmConnection();
            connection.ServiceUri = new Uri(SERVICE_URI);
            connection.ClientCredentials = new System.ServiceModel.Description.ClientCredentials();
            connection.ClientCredentials.UserName.UserName = USERNAME;
            connection.ClientCredentials.UserName.Password = PASSWORD;
            using (CrmOrganizationServiceContext context = new CrmOrganizationServiceContext(connection))
            {

                try
                {
                    Entity entity = context.Retrieve(TIME_ENTITY_NAME, GUID, new ColumnSet(TIME_QTY));

                    Console.WriteLine(entity.Attributes[TIME_QTY]);
                }
                catch (Exception e)
                {
                    Console.WriteLine( e.Message);
                }
            }
            Console.ReadKey();
            Console.WriteLine("\n");
        }

Solution

  • I figured it out. I had to take a closer look at the CrmOrganizationServiceContext object I created.

    I had to remove the entity from the cache.

    try { 
        Entity entity = context.Retrieve(TIME_ENTITY_NAME, GUID, new ColumnSet(TIME_QTY));
        context.TryRemoveFromCache(entity); // <- This fixed the cache issue.
        Console.WriteLine(entity.Attributes[TIME_QTY]); 
    }