Attaching a web resource to Microsoft CRM 2011 entities

Now we have a basic understanding of web resources, let's create a web resource of the type JavaScript that will have the function name Hello, and attach it to a Microsoft CRM 2011 entity.

Use the following steps to create a "Hello World" JavaScript web resource:

  1. Navigate to Settings | Solutions and open the MyDevelopment solution.
  2. Navigate to Web Resources and click on New.
  3. Enter the following information:
    • Name:HelloWorld.js
    • Display Name:HelloWorld
    • Type:Script (Jscript)
    • Language:English
  4. Click on Text Editor.
  5. Enter the following code under Source Field:
    function Hello()
    {
    alert('Welcome to Web Resource'),
    }
  6. Save and publish the web resource.

Now that our web resource is ready, we can attach this web resource to the entity form and associate our Hello function with the form entity event. Let's call this function in the OnLoad event of the account entity form.

First we need to add the account entity to our solution. Once the account entity is added, use the following steps to attach our web resource with the entity form:

  1. Navigate to Entities | Account | Form.
  2. Open the Account entity form.
  3. Click on the Form Properties ribbon button on the Home tab; the following screen will get displayed:
    Attaching a web resource to Microsoft CRM 2011 entities
  4. Click on the Add button to add a web resource to the entity form.
  5. Browse in our list of web resources and add the one needed to the form.
  6. Navigate to the Event Handlers section.
  7. Click on the Add button to attach our web resource function to OnLoad, as seen in the following screenshot:
    Attaching a web resource to Microsoft CRM 2011 entities
  8. Select our web resource from our library's drop-down button.
  9. Write our function name in the function text box, as shown in the following screenshot:
    Attaching a web resource to Microsoft CRM 2011 entities
  10. Click on OK to close the Event Handlers property window.
  11. Click on OK to close the form property window.
  12. Save and close the Account entity form.
  13. Publish the account entity form.

Now, when we try creating a new account record, we should get a welcome pop up as shown in the following screenshot:

Attaching a web resource to Microsoft CRM 2011 entities

We can access Microsoft CRM 2011 data in JavaScript and Silverlight web resources. Microsoft CRM 2011 provides the following two endpoints for this:

  • REST endpoint
  • SOAP endpoint

REST endpoint

REST stands for Representational State Transfer. It works in a similar way to the Internet; for example, we can access resources on the Internet by their unique URIs, and in a similar way, in REST we can access every resource by its unique URI. We can write both synchronous and asynchronous requests using REST endpoints. Microsoft CRM 2011 provides a Windows Communication Foundation (WCF) data services framework to provide OData endpoints, which is a REST-based data service and is called Organization Data Service. In Microsoft CRM 2011, we can access Organization Data Service by using the URI [Your Organization Root URL]/xrmservices/2011/organizationdata.svc.

We can only write, create, update, retrieve, delete, associate, and disassociate requests using REST endpoints. You can refer to http://msdn.microsoft.com/en-us/library/gg309549.aspxfor sample code to use REST endpoints in JavaScript and jQuery web resources.

SOAP endpoint

Microsoft CRM 2011 also provides SOAP endpoints, which provide access to all the methods on the Organization service. It does not support strong type; only the types defined in WSDL will be returned. You can refer to http://msdn.microsoft.com/en-us/library/gg594434.aspx to use SOAP endpoints in JavaScript.

Tip

You can use an SOAP logger application that comes in Microsoft CRM 2011 SDK to generate client-side SOAP request. You can find this application under the sdksamplecodecsclientsoaplogger folder in SDK.

In both endpoints, the user is authenticated by the Microsoft CRM 2011 application itself, but we cannot use these endpoints outside of the Microsoft CRM 2011 application context.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.85