Summary:

Consuming the CRM web services as a service reference rather than a web reference may result in errors when using the query or queryentity methods. Consuming the CRM web services endpoint using a service reference is currently not supported, but may be included in a future release.

Symptoms:

Service references can be used when using .NET Framework 3.0 or later. Issues can be reproduced when adding the CRM web services WSDL as a service reference (not a web reference), and WebServiceSoapPortClient rather than the standard webService.

On attempting to use the query or queryentity methods, you'll get a null reference exception when accessing anything other than the entity ID. A typical error is as follows, and was generated from the example project included with this article:

---------------------------
Object reference not set to an instance of an object.
at CRM_QueryEntity.Form1.Search() in C:\Users\crmsupport\Documents\Visual Studio 2010\Projects\CRM QueryEntity - service reference\CRM QueryEntity\Form1.cs:line 98
---------------------------

The expected result here would be that you should have access to all of the entity's properties.

Cause:

This is not supported, but there have been examples of people using this with the likes of Silverlight:

https://community.sagecrm.com/user_community/f/84/p/8741/24451.aspx#24451

 Download: CRM QueryEntity - service reference

A small VS 2010 project is attached which can be used to reproduce the issue.


Workaround:

Either use a normal web reference and the WebService class, or use queryrecord. Here's an example using queryrecord:

queryrecordRequest recordRequest = new queryrecordRequest();
recordRequest.entityname = "Company";
recordRequest.queryString = "comp_companyid=" + Convert.ToInt16(compid);
recordRequest.fieldlist = "";
recordRequest.orderby = "comp_companyid";

recordRequest.SessionHeader = new SessionHeader();
recordRequest.SessionHeader.sessionId = sessionID;

SageCRMWS.SessionHeader SH = new SageCRMWS.SessionHeader();
SH.sessionId = sessionID;

queryrecordResponse response = ObjSageWS.queryrecord(recordRequest);
ewarebase[]CRMBase = response.result.records;

crmrecord testCompany = (crmrecord) CRMBase[0];

for (int i = 0; i < testCompany.records.Length; i++)
{
    MessageBox.Show("Name: " + testCompany.records[i].name + ", Value: " + testCompany.records[i].value);
}

This is unsupported for the moment - adding this functionality to the product would require major changes to the Web Service API. This has been raised as a feature request, and may be addressed in a future release.