How to use FetchXML to retrieve entities in Dynamics CRM 2011.

FetchXML is query language based on a schema that describes the capabilities of the language. The FetchXML language supports similar query capabilities as query expression. It is used primarily as a serialized form of query expression, used to save a query as a user owned saved view in the userquery entity or as an organization owned view in the savedquery entity. A FetchXML query can be executed by using the Fetch method. There are also messages to convert between query expression and FetchXML.
Generally, the RetrieveMultiple method performs faster than the Fetch method because it does not have to parse the query.

// Set up the CRM Service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0; 
token.OrganizationName = "CRMHUNT";

CrmService service = new CrmService();
service.Url = ""http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Retrieve all accounts.
// Be aware that using all-attributes may adversely affect performance
// and cause unwanted cascading in subsequent updates.
// A best practice is to retrieve the least amount of data required.
string fetch1 = "<fetch mapping='logical'>";
fetch1 += "<entity name='account'><all-attributes/>";
fetch1 += "</entity></fetch>";

// Fetch the results.
String result1 = service.Fetch(fetch1);

// Retrieve all accounts owned by the user who has read access rights to the accounts and
// where the last name of the user is not HUNT.
string fetch2 = @"
   <fetch mapping='logical'>
      <entity name='account'>
<attribute name='accountid'/>
<attribute name='name'/>
<link-entity name='systemuser' to='owninguser'>
   <filter type='and'>
      <condition attribute='lastname' operator='ne' value='HUNT' />
   </filter>
</link-entity>
      </entity>
   </fetch>
   ";

// Fetch the results.
String result2 = service.Fetch(fetch2);
This entry was posted in MS CRM 2011 and tagged , . Bookmark the permalink.

One Response to How to use FetchXML to retrieve entities in Dynamics CRM 2011.

  1. John says:

    Hi,
    I have similar code written for CRM 4 and trying to upgrade it to 11. I can’t get the CrmService to run (not found).

    I have some working experience – old experience – with .net programming and no CRM.

    I’d appreciate some help here. Please, let me know.

Leave a Reply to John Cancel reply

Your email address will not be published. Required fields are marked *