Rish501
Rish501

Reputation: 21

Consume CRM 2011 REST data in asp.net core 3.0

I need to consume CRM 2011 data in a Blazor App (new in .net Core 3.0). SOAP no longer supported in .net Core. So looking for examples on how to consume the REST endpoint.

The Microsoft SDK assemblies all use WCF for SOAP Endpoint, which is not supported in .net core. Therefore I am forced to use the REST endpoint. I only require basic CRUD ops.

I have tried using the Unchase OData Connectedservice (https://github.com/unchase/Unchase.OData.Connectedservice/) which did generate some code. However all examples only show how to add service but haven't beed able to find any docs on how to use it? It also fails to complile with error:

<'Task' does not contain a definition for 'Factory'>

Appreciate any assistance or suggestions. Thank you

Upvotes: 0

Views: 721

Answers (1)

Rish501
Rish501

Reputation: 21

I have managed to get it working using Unchase OData Connectedservice

I had to rename the offending 'Task' with 'System.Threading.Tasks.Task'

Struggled with Async, but got it going as follows:

public static async Task<IEnumerable<Account>> ExecuteCustomersQueryAsync()
    {
        OrgContext crm = new OrgContext(new Uri("http://<Server>/<Org>/XRMServices/2011/OrganizationData.svc/"));
        crm.Credentials = new NetworkCredential("Username", "Password", "Domain");
        DataServiceQuery<Account> query = crm.AccountSet
            .AddQueryOption("$filter", "new_Class/Value eq 100000000");

        try
        {
            (ODataStandartResponse, IEnumerable<Account>) rez =  await query.ExecuteAsync();
            return rez.Item2;
        }
        catch (DataServiceQueryException ex)
        {
            throw new ApplicationException(
                "An error occurred during query execution.", ex);
        }
    }

And then to call:

 IEnumerable<Account> container = await ExecuteCustomersQueryAsync();
        try
        {
            foreach (Account cust in container)
            {
                Console.WriteLine("Customer Name: {0}, Acc No.: {1}", cust.Name, cust.AccountNumber);                   
            }
        }
        catch (DataServiceQueryException ex)
        {
                throw new ApplicationException("An error occurred during query execution.", ex);
        }

This is tested on a console app targeting netcoreapp3.0 so should work on a Blazor app. Will respond here if there any any issues.

Hope it helps anyone else looking to do same or similar.

Upvotes: 0

Related Questions