[tweetmeme source=”BenHosk” only_single=false]
I found a very interesting blog today called CRMScape. It had a fantastic article on using OData and JSON. I have used OData and JSON to retrieve CustomerAddress entity information.
You can do it if you use the JSON2.js script and modify the example code found in CRM 2011 SDK, which you can find here.
The walkthrough/tutorial below is excellent and will save you some of the frustrations I had.
The blog also has quite a few indepth development articles. Go and check out this article and others which can be found on his blog
CRM 2011 OData, JSON and CRM Forms
1. Generating OData queries
OData commands can easily be tested out in IE. To help with this, you will want to turn off the feed reading view in IE.
The first thing that you will want is the name of the Sets that you will be calling on. If you use a path like the following it will provide a listing of the names of your sets.http://crmdev:5555/CRMDEV/XRMServices/2011/OrganizationData.svc
You will see a list of them in the following format.- <collection href="AccountSet"> <atom:title>AccountSet</atom:title> </collection>
Since these names are case sensitive you will want to look at the names of your custom entities. Your stock items like AccountSet and ContactSet will be camel cased, but your custom entities will likely show up as new_myentitySet.
You can get the value from them by simply using eval like shown in the example above.
For the following CRM attributes there is more involved.
For example:var moneyValue = eval( oneEntity.new_MoneyAttribute.Value); var optionSetValue = eval ( oneEntity.new_OptionSet.Value);Setting CRM Form Fields with Queried ValuesThis gets a bit more complex when setting values to CRM form controls.
- The form field names are all lower case, so the retrieved names do not match.
- The form fields have validation and maintain more types than the returned OData values have.
- There is some conversion required between them.
You can find out the type of a form control as follows:var attrType = Xrm.Page.getAttribute("accountnumber").getAttributeType();
With the type you can then use the appropriate means to set form controls.
string, memo fields:Xrm.Page.getAttribute("accountnumber").setValue(eval(oneEntity.AccountNumber));decimal, double fields:Xrm.Page.getAttribute("new_float").setValue(parseFloat(eval(oneEntity.new_Float)));integer fieldsXrm.Page.getAttribute("new_integer").setValue(parseInt(eval(oneEntity.new_Integer)));money fieldsXrm.Page.getAttribute("new_moneyattribute").setValue(parseFloat(eval(oneEntity.new_MoneyAttribute.Value)));optionset fieldsXrm.Page.getAttribute("new_optionset").setValue(eval(oneEntity.new_OptionSet.Value));date fields
var fieldValue = eval(oneEntity.new_DateTime); var dateValue = new Date(parseInt(fieldValue.replace("/Date(", "").replace(")/", ""), 10)); Xrm.Page.getAttribute("new_datetime").setValue(dateValue);
I want to thank you first for this very detail walk through. I am very new to CRM.
My question is I added Json2 and Jquery to web Resource.
I also added them to Account.form properties.form libraries .
But when I use var jsonEntity = window.JSON.stringify(CRMObject);
I got an error : window.JSON is null or not an object
I found an explaination from below link, but I don’t know what I can do in CRM 2011. I am using IE8:
Thank you very much for any help.
have you got/ seen any examples using paging?
Fantastic beat ! I wish to apprentice while you amend your site, how can i subscribe for a weblog site? The account aided me a applicable deal. I had been a little bit familiar of this your broadcast offered brilliant clear idea
Nice, thank you. In particular, your differentiation of the multiple return (json.d.results) versus the single object (json.d) help me enormously. It also made me tell myself, duh!,