Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 24. Network Services > Consuming OData

Feedback: 0 Comments on this Section

Consuming OData

Consuming OData The OData tools that accompany the Windows Phone SDK have been much improved since the first release of Windows Phone. In the first release, an external tool had to be downloaded and used to generate the OData service proxies. In addition, the LINQ interpreter on the phone did not support closures on local variables, which meant that using a variable inside a LINQ expression caused a runtime failure. That made LINQ to OData on the phone mostly unusable. This has since been rectified, and service proxies can now be generated using the Add Service Reference dialog. Before you see how to create an OData consumer using LINQ, you should become familiar with the OData URI syntax. OData URI Structure A URI used by an OData consumer has up to three significant parts, as follows: • A service root URI • A resource path • Query string options An example of an OData URI is$top=5&$skip=5. The components of this URI are then broken down as follows: • is the service root. • /Items is the resource path. • search=phone&$top=3&$skip=5 are the query string options. If you use IE to navigate to this URI, you should see something like Figure 24.2. The page shows real eBay items for sale. The query retrieves three items—the sixth, seventh, and eighth items from a search result set—that match the phrase phone. This was done using the query string parameters search, $top, and $skip. Figure 24.2. By default, IE presents the query result using its built-in feed reader. IE has a built-in feed reader that is enabled by default. A quick way to view the XML result is to right-click on the page and select View Source. This is a nuisance when developing, however, and you may prefer to disable the feed reader instead. To disable the feed reader within IE, complete the following steps: 1. Select the Tools menu. 2. Select the Internet Options submenu. 3. Select the Content tab. 4. Click on the Settings button of Feed and Web Slices section to present the Feed Settings dialog. 5. Uncheck the Turn on Feed Reading View option. 6. Click OK, closing all dialog boxes. Once the feed reader has been switched off, the entire XML query result can be viewed with syntax highlighting, as shown in Figure 24.3. Figure 24.3. Disabling the built-in feed reader allows you to view the XML. In this case, the format of the XML is an Atom feed. Some services are capable of supporting other formats, which can be specified using the $format query option. Generating an OData Proxy To generate the proxies for the eBay OData service, right-click on the project node in the Visual Studio Solution Explorer and select Add Service Reference. The Add Service Reference is displayed, allowing you to enter the OData service URL (see Figure 24.4). Figure 24.4. Add Service Reference dialog is used to generate the OData service proxies. Tip Changing the namespace of the generated service proxy and entity classes can be achieved by selecting Show All Files in the Visual Studio Solution Explorer and expanding the Service Reference node. Change the namespace by setting the Custom Tool Namespace property in the properties of the Reference.datasvcmap file using the Visual Studio properties pane. OData Query Options To retrieve data from an OData service, a URI is constructed that specifies the resources that you are interested in—for example, Products—and you restrict the items returned by the service using various query options. The following are the three types of OData query string options: • System query options—System query options are used to control the amount and order of the data that an OData service returns for the resource identified by the URI. The names of all System Query Options are prefixed with a “$” character. Not all system query options need to be supported by a producer. If not supported, the server should return an HTTP status code 405 (Method Not Allowed). The following is a summary list of OData system query options defined by the standard: • $orderby—This option allows you to order the results of a query by an entity property. For example, to retrieve the list of eBay product categories ordered by Name, the following query could be used:$orderby=Name. • $top—This option allows you to select the first N items from a result set—where N is positive integer specified in the query. For example, the following query selects the first five items from the complete list of eBay items:$top=5. • $skip—This option allows you to select items that occur after a particular index in the result set and is useful when performing paging. By default, an OData service usually limits the number of items returned at any one time. In the case of the eBay OData service, this limit is 500 items. The $skip option, in conjunction with the $top option, can be used to page through results. • $filter—This option allows you to restrict the results to those matching the specified criteria. The $filter option supports various logical, arithmetic, and grouping operators, as well as a set of functions such as substringof. See the OData website for more information on the $filter option. • $expand—This option allows you to retrieve a graph of entities. For example, rather than retrieving the first three product categories and then the products within those three categories in another query, by using the $expand option you are able to retrieve them all at once. This is done as shown in the following example:$top=3&$expand=Products. The syntax of an $expand query option is a comma-separated list of navigation properties. Additionally, each navigation property can be followed by a forward slash and another navigation property to enable identifying a multilevel relationship. • $format—This option allows you to specify the format of the response. Valid $format values include Atom, XML, and JSON. The $format value may also be set to a custom value specific to a particular OData service. • $select—This option allows you to retrieve only the data that you are interested in and can be important for performance when working with OData on the phone. The value of a $select option is a comma-separated list of selection clauses. Each selection clause may be a property name, navigation property name, or the “*” character. To select only the names of the top five search results for the phrase phone, the following query could be used:$top=5&$select=Title. • $inlinecount—This option allows you to retrieve a count of the number of items in a result set, along with the entity elements. When combined with a $top option of 0 (specified by &$top=0), you can retrieve the number of items in a set without having to go to the trouble of downloading unnecessary bytes. • Custom query options—Custom query options are specific to the OData service that uses them. They differ in that they are not prefixed by a $ symbol. The following is an example of a custom query option, where xname is the parameter name, and xvalue is its value: • Service operation parameters—A service operation is a function specific to the particular OData service. Service operation parameters work much like custom query options, where key/value pairs are provided to a custom function as arguments. For example, a service operation exists on the OData sample website that retrieves all products with a particular rating. The rating is specified using the rating service operation parameter, as shown:

You are currently reading a free preview of this book.


Sign up for a
Safari Library subscription
to access Rough Cuts.


Subscribe Now