Creating Links to CRM Records from External Systems Using the CRM 2011 REST Services

Visit Website View Our Posts

A common scenario with a CRM implementation includes the need to link to CRM from an external system.  The CRM SDK actually describes the URL format for accessing CRM Forms using a URL at the following link, Open Forms, Views, and Dialogs with a URL.  Great, problem solved!  However, if you take a close look at the URL for a CRM Form you will see that it takes the following format.


As you can see, the problem that presents itself is the need for the GUID of the record.  However, quite often the external system/page linking into CRM may not have access to the GUIDs for CRM records.  One solution to this problem is to use a tool to get the GUIDs for the CRM records into the external system.  A better solution is to leverage CRM Web Resources and the REST services to create a HTML page that can redirect the user to the proper record given some identifying information from the external system, such as an external identifier from the system that is synchronized and stored in CRM.

The first thing needed is to create a HTML Web Resource within CRM.  This is a very simple page that merely lets the user know that they are being redirected to a CRM record.

1: <html>

2: <head>

3: <title>Query Redirection</title>

4: <script src="../scripts/json2.min.js" type="text/javascript"></script>

5: <script src="../scripts/jquery_1.6.1.min.js" type="text/javascript"></script>

6: <script src="../scripts/jquery.xrm.orgData_1.0.1.min.js" type="text/javascript"></script>

7: <script src="../../../../ClientGlobalContext.js.aspx" type="text/javascript"></script>

8: <link href="../styles/RedirectQuery.css" rel="stylesheet" type="text/css" />

9: </head>

10: <body>

11: <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">

12: <tr>

13: <td id="contentContainer" align="center" valign="middle">

14: <div class="content">

15: <img src="../images/spinner_circle.gif" />

16: <div>Searching for record...</div>

17: </div>

18: </td>

19: </tr>

20: </table>

21: <script src="../scripts/RedirectQuery.js" type="text/javascript"></script>

22: </body>

23: </html>

As you can see, I have included some JavaScript files to make things a little easier when calling the REST services.  Also, notice the inclusion of the ClientGlobalContext.js.aspx script.  This script file gives the Web Resources the needed XRM context, such as the Xrm.Page.context object.  The last script included at the bottom of the page, i.e. RedirectQuery.js is where all of the magic is going to happen.  Let’s take a look at what this script needs to do.

The RedirectQuery.js script is going to take a REST query that is passed as a query string parameter, execute the query, extract the logical name of the entity and the ID (GUID) of the record from the result, and redirect the user to the CRM Form for the record.

The first piece to this puzzle is to get the REST query from the query string.  HTML Web Resources in CRM are only allowed to have a single query string parameter called “data”, so the REST query is going to be encoded in this query string parameter.

1: // Get the query parameters data

2: queryData = decodeURIComponent(getQueryParam("data"));

Now that we have our REST query we can execute it using a bit of jQuery and a homegrown jQuery plugin that wraps up the calls to the REST services.

1: // Get the data for the target record

2: targetData = $.xrm.orgData.retrieveMultiple({ async: false, query: targetQuery });

The next step is to extract the logical entity name from the original query and the ID (GUID) of the entity from the first record in the results of the REST query.

1: // Get the entity type name from the supplied target query

2: targetEtn = targetQuery.match(/^([a-zA-Z0-9_]+?)Set/)[1];


4: // Extract the ID from the data

5: targetId = targetData[targetEtn + "Id"];

Finally, we can build the URL to the CRM Form with the data we have extracted from our original query and the query’s results and redirect to the CRM Form.

1: // Build the URL

2: targetUrl = Xrm.Page.context.getServerUrl().replace(/\/$/, "") + "/main.aspx" +

3: "?etn=" + targetEtn.toLowerCase() +

4: "&id=" + encodeURIComponent(targetId.toLowerCase()) +

5: "&pagetype=entityrecord";


7: // Redirect to the CRM Form

8: window.location.replace(targetUrl);

As you can see, this CRM Web Resource can help solve the problem of linking to CRM Forms from an external system.  All that is left to do is to create the REST query and link on the external web page.  The REST query used can contain any criteria necessary to retrieve the desired CRM record.  The criteria is commonly an external identifier stored in CRM, but the possibilities are only limited by the capabilities of the CRM REST services.

Post by: Nick Doriot, Customer Effective

1 thought on “Creating Links to CRM Records from External Systems Using the CRM 2011 REST Services”

  1. Thank you for your guide. I need some help though.
    I am not sure how to create the REST query.

    What I don't understand is this line:
    queryData = decodeURIComponent(getQueryParam(“data”));

    And also, what should be in this file, I can't find it anywhere?


Comments are closed.