Execute fetchxml queries in javascript using XrmLinq
Javascript is a popular method of accessing and displaying data in crm. We’ve created a javascript library that you can re-use to make it easier.
Let’s take a look at how easy it is to execute a fetchxml query and display the results.
<script type="text/javascript">
function FetchExample() {
// create a connection to the server
var sdk = new XrmDataContext("http://xrmlinq-dev01", "XrmLinqDev");
 
var fetchXml = " <fetch mapping='logical'> <entity name='contact'><attribute name='contactid'/><attribute name='fullname'/> </entity></fetch>";
var result = sdk.Fetch(fetchXml); // execute the query
for (var i = 0; i < result.length; i++) {
alert(result[i].attributes["name"]); // display the results
}
}
</script>
Let’s make it even better, if you hook into the query translator in XrmLinq you can get the generated fetchxml for linq queries.
Here’s an example.
// connection to crm
CrmDataContext context = new CrmDataContext(...);
string fetchXml = (from a in context.Accounts
where a.AccountId == new Guid()
select a.Name).ToFetchXml();
Resulting fetchxml query is
<fetch mapping="logical" page="1" count="1">
<entity name="account">
<attribute name="name" />
<filter type="and">
<condition attribute="accountid" operator="eq" value="00000000-0000-0000-0000-000000000000" />
</filter>
</entity>
</fetch>
You can use the generated fetchxml query in javascript. We hope this has been useful to you, if you’d like to download our javascript library please contact us.





Try XrmLinq
By Charles L, December 20, 2009 @ 3:33 am
Can you please send me a copy? Thanks