Sample: Retrieve Multiple Records Using the REST Endpoint with Silverlight

[Applies to: Microsoft Dynamics CRM 2011]

This sample code is for Microsoft Dynamics CRM 2011, and can be found in the following location in the SDK download:

SDK/SampleCode/CS/Silverlight/RESTRetrieveMultipleSilverlight

If you just want to see how this sample works you can install (import) the RESTRetrieveMultipleSilverlight_1_0_0_0_managed.zip managed solution included in the download files. If you install this managed solution and want to create the Web resources using the names below, your solution publisher customization prefix cannot be “sample” unless you uninstall (delete) the managed solution.

An HTML page can be used to view the Microsoft Silverlight control outside a form. The only purpose of this Web resource is to provide the URL of the server when the Silverlight control cannot access it itself through the Xrm.Page.context.getServerUrl function when the Silverlight Web resource is added in an entity form.

Note

The customization prefix ” sample_” is not used in code. These samples will work using the customization prefix from any publisher. However, the relative path of the simulated Scripts and Styles folder must be included in the name of the Web resources.

Demonstrates

By default the number of records returned by the REST Endpoint is limited to 50 records. This sample shows how to retrieve multiple records without regard to this limitation.

This sample demonstrates how a user-defined object (PagingContext) can be used to transfer state between the start of the operation and the callback defined by ProcessPages. The first two properties of PagingContext (Query and ServiceContext) are required in order to navigate through the pages. When instantiated, the last property (PageProcessor) is set to a delegate function that will be called for each set of records to process the results. PageProcessor contains the actions to be performed in the application. In this case to simply add more account records to the AccountCollection variable.

The ProcessPages callback method determines if any more records exist and if they do, it uses the DataServiceQueryContinuation(T) Class. The DataServiceQueryContinuation class contains a single property, NextLinkUri which provides URL that contains the $skiptoken parameters that describes the paging boundary. This DataServiceQueryContinuation instance is passed through using the crmContext.BeginExecute method along with the ProcessPages callback method and the context information containing information about the state of the operation.

Although this sample uses the PagingContext and ProcessPages classes to retrieve account records, they can be used for any type of entity.

sample_/ClientBin/RESTRetrieveMultipleSilverlight.xap

This Silverlight file is compiled from files in a Microsoft Visual Studio 2010 Silverlight application (version 4) project named ‘RESTRetrieveMultipleSilverlight’. The only files that were created or modified are as follows:

sample_/RESTRetrieveMultipleSilverlightTestPage.html

This HTML Web resource was created from the RESTRetrieveMultipleSilverlightTestPage.html page created with the Silverlight application project. It has only been edited to replace the default <script src="Silverlight.js" type="text/javascript"></script> script reference with <script type="text/javascript" src="../ClientGlobalContext.js.aspx"></script> so that the Silverlight application will have access to the application context.