Showing posts with label mscrm. Show all posts
Showing posts with label mscrm. Show all posts

Tuesday, March 20, 2012

CRM 2011 and OData: Retrieve Example Using Async and Sync Methods

I've found in my transition from CRM 4 to CRM 2011 the ability to use REST and SOAP to be the two biggest improvements to developers. The benefit of both is essentially making the CRM 2011 UI fluid similiar to AJAX. In fact, this example used an AJAX call.

In this example I have successfully used oData to query the db and return a value which can be used to validate ribbon buttons, data, etc.

At the moment this is only configured to retrieve but we can easily extend to create and update.

retrieveRecord: function (id, odataSetName, successCallback, errorCallback) {
        //GetGlobalContext function exists in ClientGlobalContext.js.aspx so the
        //host HTML page must have a reference to ClientGlobalContext.js.aspx.
        var context = this.GetCrmContext();
        //Retrieve the server url, which differs on-premise from on-line and
        //shouldn't be hard-coded.
        var serverUrl = context.Page.context.getServerUrl();
        //The XRM OData end-point
        var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";

        //id is required
        if (!id) {
            alert("record id is required.");
            return;
        }
        //odataSetName is required, i.e. "AccountSet"
        if (!odataSetName) {
            alert("odataSetName is required.");
            return;
        }
        if (successCallback != null) {
            //Asynchronous AJAX function to Retrieve a CRM record using OData
            $.ajax({
                type: "GET",
                contentType: "application/json; charset=utf-8",
                datatype: "json",
                url: serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')",
                beforeSend: function (XMLHttpRequest) {
                    //Specifying this header ensures that the results will be returned as JSON.            
                    XMLHttpRequest.setRequestHeader("Accept", "application/json");
                },
                success: function (data, textStatus, XmlHttpRequest) {
                    if (successCallback) {
                        successCallback(data.d, textStatus, XmlHttpRequest);
                    }
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    if (errorCallback)
                        errorCallback(XmlHttpRequest, textStatus, errorThrown);
                    else
                        errorHandler(XmlHttpRequest, textStatus, errorThrown);
                }
            });
        }
        else {
            var request = new XMLHttpRequest();
            request.open("GET", serverUrl + ODATA_ENDPOINT + "/" + odataSetName + "(guid'" + id + "')", false);
            request.setRequestHeader("Accept", "application/json");
            request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            request.send();
            var $objJquery = jQuery.parseJSON(request.responseText);
            return $objJquery.d;
        }
    }
 GetCrmContext: function () {
        var params = null;
        var myXrm = null;
        // Get XRM object (if possible)
        try {
            if (typeof (Xrm) != 'undefined' && Xrm != null) {
                myXrm = Xrm;
            }
        }
        catch (ex) {
            // DO NOTHING
        }
        return myXrm;
    }


The method has 4 parameters, 2 of which are required:

1.       The guid of the record. This is found by using the following command:

a.       var _id = Xrm.Page.data.entity.getId();

2.       string of the entity set. i.e. new_AccountSet. As with all javascript, proper capitalization is required.

3.       (optional) success Callback method. If you want to run async, provide a method and be ready to handle the return object.

4.       (optional) failure Callback method. Also, if async, provide a method for failure.



NOTE: If you do not set callback methods, then the method will try to run the query inline. If successful you’ll have to provide it something to set:

                var _id = Xrm.Page.data.entity.getId();
                var objAccount = retrieveRecord(_id, "new_AccountSet", null, null);

If you choose to provide a success method, if might look something like this:

function retrieveEntityCompleted(data, textStatus, XmlHttpRequest) {
    _recordData = data;
}

Tuesday, April 26, 2011

Importing Solution to Another Organization Changes ObjectTypeCode

An issue came up yesterday regarding importing one organization's customizations (sitemap, isv.config, custom entities, etc.) into another. While the files imported successfully and everything appeared correct we quickly realized one crucial error we had assumed...

When importing a solution to another organization changes the ObjectTypeCode of every custom entity!

The ObjectTypeCode is the database equivalent of the ETC query string parameter. In simpler terms, any reference to the etc code in the URL of any custom entity will be in correct.
The issue it seems is that when importing, CRM ignores the ObjectTypeCode node in the customizations.xml and instead inserts them alphabetically instead.

So if you had an entity named new_account and it had an etc of say 10100 which typically means its the one hundredth custom entity created, it will now be 10000 since it is the first alphabetically.

So what's the solution?

Some workarounds mentioned around the web and in the msdn forums is to use the ObjectTypeName, abbreviated OTN. If you notice when crm generates the url for a custom entity it adds this as a query string parameter labeled objecttypename=new_account.

So by using this method you avoid using the etc and refer to the string equivalent. I have always assumed this etc number to carry over since its in the xml but its not.


Happy Coding!

Tuesday, April 5, 2011

TechNet Online Documentation for MSCRM 2011

Link:
http://technet.microsoft.com/en-us/library/bb496811.aspx

The link above gives you access to the technet documentation.
The navigation to the left will allow to choose from the following:

  • Installation
  • SDK
  • White Papers
The Installation section includes the following:
  • Introduction
  • Planning Guide
  • Installing Guide
  • Operating and Maintaining Guide
The SDK section links to this page which will give developers sample code and quickstart solutions. The Link: http://technet.microsoft.com/en-us/library/gg309408.aspx

The White Papers section currently only houses the Claims Based Authentication but will be expanded soon.

This is a valuable go to resource for any CRM developer!

Happy Coding!

                                              

Thursday, December 9, 2010

ISVConfig/SiteMap Tip #1: Point to an ASPX page to extend functionality

So far it's been about 2 months since I've been working with the CRM 2011 Beta and while many things have changed drastically, the SiteMap and ISVConfig still adhere to the same rules as in CRM 4.

When creating a link for the Workplace I prefer to use an ASPX page as opposed to a JS page or otherwise. The reason I use it is not only can I embed JS I can also render my own webpages, silverlight controls, etc.

In a real world example,  you are tasked with creating a link in the workplace that not only points to the Active View of the desired entity but also opens a new window that allows you to create an instance of that particular entity. I pointed my SiteMap to an ASPX page that simply ran JS onLoad:

<beginscript language="javascript" type="text/javascript">
//Open popup and set location using CRM global var
window.open('/' + parent.top.ORG_UNIQUE_NAME + '/userdefined/edit.aspx?etc=10002', null);
window.location.href =
'/' + parent.top.ORG_UNIQUE_NAME + '/_root/homepage.aspx?etc=10002&pagemode=iframe';</endscript>


*Note: Replace the begin and endscript tags with script. Blogger apparently doesn't like that tag!

However if I needed some silverlight or a webpage I can easily modify the ASPX page outside of CRM and not have to worry about importing. Using this method provides me enough abstraction to pretty much do what I want.

Tips about the code above:
  • Using the CRM provided JS variables helps out tremendously. Familiarize yourself with them!
  • The pagemode=iframe parameter in the url will give you the correct view to show in the window.location. It removes the ribbon and anything else besides the core view.