Saturday, June 4, 2011

Dynamics crm 2011 javascript and crmrestkit

Dynamics crm 2011 javascript and crmrestkit
I find crmrestkit the easiest and fastest way to work with rest service in Microsoft Dynamics crm 2011. It's really simple, straight forward and supports both synchronous and asynchronous service calls.
In order to get started you should get:

Add them to your Microsoft Dynamics crm 2011 Web resources:

Last step is to add them to the CRM form.
Now everything is ready for using the crmrestkit.
Few samples:
1) Retrieve multiple - synchronous :
var columns = ['new_attributeId', 'new_attributeName'];
var filter = "new_attributeName eq 'WOW'";
var collection = CrmRestKit.RetrieveMultiple('EntityName', columns, filter);
if (collection != null && collection.results != null && collection.results.length > 0) {
var valId = "{" + collection.results[0].new_attributeId + "}";
var name = collection.results[0].new_attributeName;
var attribute =Xrm.Page.getAttribute("new_attribute1");
attribute.setValue([{ id: valId, name: name, entityType: "EntityName"}]);
}
2) Retrieve multiple – Asynchronous
var columns = ['new_attributeId', 'new_attributeName'];
var filter = "new_attributeName eq 'WOW'";
CrmRestKit.RetrieveMultiple('EntityName', columns, filter,  function (collection) {
if (collection != null && collection.results != null && collection.results.length > 0) {
 var valId = "{" + collection.results[0].new_attributeId + "}";
var name = collection.results[0].new_attributeName;
var attribute =Xrm.Page.getAttribute("new_attribute1");
attribute.setValue([{ id: valId, name: name, entityType: "EntityName"}]);
}
}); 
As you can see it is simple to read, write and understand.
When working with rest services remember that entities and attributes schema names are case sensitive.
In order to get the right name go to:
CRM website->setting->customization->developer resources-> Organization Data Service.
Or just go to SERVER URL/XRMServices/2011/OrganizationData.svc

Here you can find the names of the Entities, For the Attribute name just add the Entity name to the end of the link Example: SERVER URL/XRMServices/2011/OrganizationData.svc/IncidentSet()
In order to see Rest results go to RSS setting in IE and uncheck the "Automatically mark feed …"























Another great way for building the queries without mistakes is using the "Crm 2011 Odata query designer":

http://bingsoft.wordpress.com/2011/03/06/crm-2011-odata-query-designer-crm2011/




12 comments:

  1. Hi,

    Can we use this toolkit in MS CRM 2011 with custom entity.

    Thanks

    ReplyDelete
  2. Thanks Yairrose..Thanks alot

    You are Toolkit helps me a lot.

    ReplyDelete
  3. Hi,

    How can we pass filter for radio buttons,check boxes in crm 2011.

    i am getting error while passing filter on radio button field.

    ReplyDelete
  4. Hi,
    In order to easily create quries you could use the "CRM 2011 OData Query Designer":

    http://bingsoft.wordpress.com/2011/03/06/crm-2011-odata-query-designer-crm2011/

    About the radio buttons:
    (For false)
    http://crm2011:5555/Basic/xrmservices/2011/OrganizationData.svc/AccountSet?$filter=DoNotFax eq false

    (For true)
    http://crm2011:5555/Basic/xrmservices/2011/OrganizationData.svc/AccountSet?$filter=DoNotFax eq true

    ReplyDelete
  5. Hi,

    I am getting error Bad Request when i m passing special chr in filter condition as ...
    /XRMServices/2011/OrganizationData.svc/ContactSet()?$select=ContactId&$filter=FirstName eq 'D'soza'

    if i pass simple name like FirstName eq 'Dsoza' then it works fine.

    Best Regards,
    Smith

    ReplyDelete
  6. I think you should URL encode the special chr, try using the JavaScript:escape command. I'll try it later on and let you know.

    ReplyDelete
  7. Hi
    Nice blog.It help to create a quality code.Thanks for teach me very good information.It was so helpful.Great effort.

    ReplyDelete
  8. Hi Yairrose,

    Nice Blog.
    I'm using Restkit in most of my operations and it works fine.
    but while creating Activity Party record i'm not able to create record with multiple records in Activity Party.
    For e.g i'm trying to create email record with multiple receivers.
    Please give me any suggestion on same.

    Your reply must appreciated.

    Regards,
    Mahesh

    ReplyDelete
    Replies
    1. Hi,
      Thank you.
      I guess it's all about how you build the JSON.
      I've looked for some other blogs that might help you, I just don't have time right now to try it myself.

      http://social.microsoft.com/Forums/en/crmdevelopment/thread/f3e68e94-2d39-4c99-a88c-d90c0fb1b871

      http://mscrmshop.blogspot.com/2011/09/how-to-create-email-activity-using-rest.html

      Let me know if it works.
      Regards,
      Yair

      Delete
  9. Very interesting post….really informative…..of all the blogs I have read on the same topic, this one is actually enlightening….I was longing to read such kind of informative write-ups from a very long time…good to know that such kind of posts regarding.
    eCommerce Development Services Company in Bangalore | Magento developers in Bangalore

    ReplyDelete
  10. Nice blog.It help to create a quality code.Thanks for teach me very good information.It was so helpful.Great effort.
    Ecommerce Website Design and Development Company In Bangalore|Lead Magento Ecommerce Development company

    ReplyDelete