[Applies to: Microsoft Dynamics CRM 4.0]
Find the latest SDK documentation: CRM 2015 SDK
Fetch is a proprietary query language that is used in Microsoft Dynamics CRM. It is 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.
Microsoft Dynamics CRM 4.0 supports a new attribute on the link-entity node called visible. Setting visible to false will hide the linked entity in the advanced find user interface. It will still participate in the execution of the query and will return the appropriate results.
The following table lists the messages that work with the FetchXml language.
|ExecuteFetch||Executes a query.|
|FetchXmlToQueryExpression||Converts from FetchXML to query expression.|
|QueryExpressionToFetchXml||Converts from query expression to FetchXML.|
|ValidateSavedQuery||Validates a saved query (view).|
Warning You should avoid using all-attributes in a fetch query because of the impact on a subsequent update of records. In an update, this will set all field values, even if they are unchanged, and often triggers cascaded updates to child records.
The following code examples show how to use FetchXML. The first FetchXML statement retrieves all accounts. The second retrieves all accounts where the last name is not equal to Cannon. Notice that in either case, the privileges of the logged on user will affect the set of records returned. The Fetch method only retrieves records for which the logged on user has read access.
[C#] // Set up the CRM Service. CrmAuthenticationToken token = new CrmAuthenticationToken(); token.AuthenticationType = 0; token.OrganizationName = "AdventureWorksCycle"; 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 Cannon. 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='Cannon' /> </filter> </link-entity> </entity> </fetch> "; // Fetch the results. String result2 = service.Fetch(fetch2); [Visual Basic .NET] ' Set up the CRM Service. Dim service As New CrmService() 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. Dim fetch1 As String = _ "<fetch mapping='logical'>" + _ "<entity name='account'>" + _ "<all-attributes/>" + _ "</entity>" + _ "</fetch>" ' Fetch the results. Dim result1 As String = 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 Cannon. Dim fetch2 As String = _ "<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='Cannon' />" + _ "</filter>" + _ "</link-entity>" + _ "</entity>" + _ "</fetch>" ' Fetch the results. Dim result2 As String = service.Fetch(fetch2)
- Building Queries
- Blog: Stunnware Tools Framework Fetch XML Plug-in
- Blog: Fetch All Records
- Blog: Retrieving All Records with FetchXML
- CodePlex Sample: FetchXML Builder