Professional Documents
Culture Documents
Dynamics 365
FetchXML is a XML based query language used in Microsoft Dynamics 365 CRM to fetch data. FetchXML is
capable of doing many things as explained below.
Can only be used to retrieve data not able to perform CUD (Create/Update/Delete) operation.
Can be used in JavaScript to retrieve data in client side also used in server side code to retrieve data.
Supports Linked entity data retrieval with conditions, filters, GroupBy, Sorting
Can fetch 5000 record data at a time and supports paging concept to fetch more records page by page.
Can be saved in user query entity as user owned view and saved query entity as organization owned
view.
Every day we use fetchXML in development. So here i have given a list of ready-made fetchXMLs which
you can use by modifying the code as per your business requirement. FetchXML query should validated
by the xsd schema.
Retrieve all attributes of account entity
<fetch
mapping=’logical’>
<entity
name=’account’>
<all-attributes/>
</entity>
</fetch>
<entity
name=’account’>
<attribute
name=’accountid’/>
<attribute
name=’name’/>
</entity>
</fetch>
<entity
name=’account’>
<attribute
name=’accountid’/>
<attribute
name=’name’/>
<filter
type=’and’>
<condition
attribute=’name’
operator=’ne’
value=’Softchief’
/>
</filter>
</entity>
</fetch>
<entity
name=’contract’>
<attribute
name=’title’/>
<attribute
name=’customerid’/>
<link-entity
name=’contact’
from=’contactid’
to=’customerid’>
<filter
type=’and’>
<condition
attribute=’lastname’
operator=’eq’
value=’Pradhan’
/>
</filter>
</link-entity>
</entity>
</fetch>
<entity
name=’contact’>
<attribute
name=’name’
/>
</entity>
</fetch>
<entity
name=’contact’>
<attribute
name=’name’
/>
<attribute
name=’lastname’
/>
</entity>
</fetch>
Use of Inner Join in FetchXML
<fetch
version=’1.0′
mapping=’logical’
distinct=’false’>
<entity
name=’contact’>
<attribute
name=’fullname’/>
<link-entity
name=’account’
to=’parentcustomerid’
from=’accountid’
link-type=’inner’>
<attribute
name=’name’/>
</link-entity>
</entity>
</fetch>
<entity
name=’account’>
<attribute
name=’name’
/>
<link-entity
name=’lead’
from=’leadid’
to=’originatingleadid’
link-type=’outer’
/>
<filter
type=’and’>
<condition
entityname=’lead’
attribute=’leadid’
operator=’null’
/>
</filter>
</entity>
</fetch>
<entity
name=’opportunity’>
<attribute
name=’estimatedvalue’
alias=’estimatedvalue_avg’
aggregate=’avg’
/>
</entity>
</fetch>
<entity
name=’opportunity’>
<attribute
name=’name’
alias=’opportunity_count’
aggregate=’count’/>
</entity>
</fetch>
Using “older than” clauses for date and time values in FetchXML Query
<fetch>
<entity
name=”incident”>
<attribute
name=”title”
/>
<attribute
name=”ticketnumber”
/>
<attribute
name=”createdon”
/>
<attribute
name=”incidentid”
/>
<filter
type=”and”>
<condition
attribute=”createdon”
operator=”olderthan-x-minutes”
value=”40″
/>
</filter>
</entity>
</fetch>
<entity
name=”order”>
<attribute
name=”totalamount”
aggregate=”sum”
alias=”total”/>
<attribute
name=”datefulfilled”
groupby=”true”
dategrouping=”fiscal-period”/>
</entity>
</fetch>
<entity
name=’opportunity’>
<attribute
name=’opportunityid’
alias=’opportunity_count’
aggregate=’count’/>
<attribute
name=’estimatedvalue’
alias=’estimatedvalue_sum’
aggregate=’sum’/>
<attribute
name=’estimatedvalue’
alias=’estimatedvalue_avg’
aggregate=’avg’/>
<attribute
name=’actualclosedate’
groupby=’true’
dategrouping=’quarter’
alias=’quarter’
/>
<attribute
name=’actualclosedate’
groupby=’true’
dategrouping=’year’
alias=’year’
/>
<order
alias=’year’
descending=’false’
/>
<order
alias=’quarter’
descending=’false’
/>
<filter
type=’and’>
<condition
attribute=’statecode’
operator=’eq’
value=’Won’
/>
</filter>
</entity>
</fetch>
<entity
name=’opportunity’>
<attribute
name=’opportunityid’
alias=’opportunity_count’
aggregate=’count’/>
<attribute
name=’estimatedvalue’
alias=’estimatedvalue_sum’
aggregate=’sum’/>
<attribute
name=’estimatedvalue’
alias=’estimatedvalue_avg’
aggregate=’avg’/>
<attribute
name=’actualclosedate’
groupby=’true’
dategrouping=’quarter’
alias=’quarter’
/>
<attribute
name=’actualclosedate’
groupby=’true’
dategrouping=’year’
alias=’year’
/>
<order
alias=’year’
descending=’false’
/>
<order
alias=’quarter’
descending=’false’
/>
<filter
type=’and’>
<condition
attribute=’statecode’
operator=’eq’
value=’Won’
/>
</filter>
</entity>
</fetch>
FetchXML
Operator ConditionOperator Description
How to Retrieve Data using Fetch XML in server side (Sample given below)
// Retrieve all accounts owned by the user with read access rights to the accounts and
string varFetchXML = @”
<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> “;
EntityCollection result =
_serviceProxy.RetrieveMultiple(new FetchExpression(varFetchXML));
System.Console.WriteLine(c.Attributes[“name”]);
“<fetch
version=’1.0′
output-format=’xml-platform’
mapping=’logical’
distinct=’false’>”+
“<entity
name=’account’>”+
“<attribute
name=’name’
/>”+
“<attribute
name=’primarycontactid’
/>”+
“<attribute
name=’telephone1′
/>”+
“<attribute
name=’accountid’
/>”+
“<order
attribute=’name’
descending=’false’
/>”+
“<filter
type=’and’>”+
“<condition
attribute=’name’
operator=’like’
value=’C%’
/>”+
“</filter>”+
“</entity>”+
“</fetch>”;
req.setRequestHeader(“Accept”, “application/json”);
req.onreadystatechange = function ()
if (this.readyState === 4)
this.onreadystatechange = null;
else
alert(this.statusText);
};
req.send();
string fetchNotifications = @”
<entity name=’new_notification’>
<filter type=’and’>
<condition attribute=’new_contact’ operator=’eq’ value='”+ contactId + @”‘/>
</filter>
</entity>
</fetch> “;
1. In Visual Studio, create or open a Report Server project. For more information,
see Create a Report Server Project.
2. On the Project menu, choose Add New Item.
4. Choose the TFS2010ReportDS shared data source, and then choose Next.
Even though you might have installed or upgraded to TFS 2013, these names,
which were assigned to the data sources for TFS 2010, are in use.
To create the query that will retrieve the data for the report
1. Choose Generic Query Designer on the query builder toolbar to enable the query
designer.
2. Choose Add Table on the query builder toolbar.
3. Choose the Current Work Item table, and then choose Add.
4. Choose the Work Item table, and then choose Add.
This table contains the Work Item dimension.
5. Choose the Person table, choose Add, and then click Close.
This table contains the Person dimension. The fact table for current work items has
foreign keys to this table for the Assigned To, Changed By, and Created By fields.
SQLCopy
9. Choose Run on the query builder toolbar to verify that the query works, and then
choose OK.
The Query Builder is closed, and the Design the Query page of the Report
Wizard reappears.
1. Choose Next.
8. Type a name for the report, choose Preview Report, and then choose Finish to
create the report.
The wizard closes, and the report document window appears with the Preview tab
active.
To successfully deploy the report, your project settings must be set to appropriate
values. For more information, see Create a Report Server Project.