Export (0) Print
Expand All

Server-Side Data Filtering

Ff750808.note(en-us,MSDN.10).gifNote:
Current information about Live Connect is now available in the Windows Live Developer Center. The information in the following sections is provided for legacy purposes only.

Use server-side filtering when you do not need to retrieve all of the data for a particular resource, or when the criteria for filtering the data are known in advance.

Server-side filtering is accomplished during the loading of resources from the Messenger Connect endpoint. The filtering takes place at the endpoint. The result of the filter operation is resource data that is returned to your web application with a data context. You specify the filter in the options parameter of the load method of the DataContext class. Compare this to the loadAll method, which loads all resources.

Query parameters are case sensitive. Specify operators in lowercase, as shown in the following example.

var options = { "$filter": "FirstName eq 'William' and LastName eq 'Flash'" };

The comparison of filter values to resource data is also case sensitive. For example, filtering for "william" returns no results if the data collection contains only values equal to "William".

The following example shows how to concatenate multiple query operators. Each new operator is preceded by a comma and added to the list, in accordance with JavaScript Object Notation (JSON) format.

var options = { "$top": "10", "$filter": "ActivityType eq 'AddCommentActivity'" };

Queries are not supported on properties of the enumeration type; for example, Microsoft.Live.Services.ContactUrlType.

For a list of supported operators, see Querying Resources.

The following code example demonstrates how to load the currently authenticated user's activities and request only the ten most recent activities. Note that the Load method is used instead of the LoadAll method.

function listContacts() {
    var options = { "$top": "10" };
    dataContext.load(Microsoft.Live.DataType.contacts, options, contactsLoaded);
}

function contactsLoaded(dataLoadCompletedEventArgs) {
    Sys.Debug.trace("Top 10 Contacts Loaded Using Server-Side Filtering:");
    var contactsCollection = dataLoadCompletedEventArgs.get_data();
    for (var i = 0; i < contactsCollection.get_length(); i++) {
        var contact = contactsCollection.get_item(i);
        var title = contact.get_title();
        Sys.Debug.trace("Contact " + i + ": " + title);
    }
}

The following sample shows a complete HTML page that filters a contact list by using server-side filtering.

To test this page:

  1. Follow the instructions in Getting Started Sample for ASP.NET.
  2. Create a new ASP.NET page in the sample project.
  3. Replace the <html> element in the ASP.NET page with the sample code shown after this procedure.
  4. Update the sample code by providing your client ID in the <wl:app> tag.
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wl="http://apis.live.net/js/2010">
<head>
    <title>Server-Side Data Filtering</title>
    <script type="text/javascript" src="http://js.live.net/4.0/loader.js"></script>

    <script type="text/javascript">
        var dataContext;

        function appLoaded(evtArgs) {
            Microsoft.Live.Core.Namespace.using("wl:Microsoft.Live");
        }

        function signInCallback(signInCompletedEventArgs) {
            if (auth.get_state() == Microsoft.Live.AuthState.failed) {
                alert("Authentication failed.");
                return;
            }
            else if (auth.get_state() == Microsoft.Live.AuthState.authenticated) {
                Sys.Debug.trace("JS_snippet_client_0_Template: Signin succeeded.");
                dataContext = wl.App.get_dataContext();
                listContacts();
            }
        }

        function listContacts() {
            var options = { "$top": "10" };
            dataContext.load(Microsoft.Live.DataType.contacts, options, contactsLoaded);
        }

        function contactsLoaded(dataLoadCompletedEventArgs) {
            Sys.Debug.trace("Top 10 Contacts Loaded Using Server-Side Filtering:");
            var contactsCollection = dataLoadCompletedEventArgs.get_data();
            for (var i = 0; i < contactsCollection.get_length(); i++) {
                var contact = contactsCollection.get_item(i);
                var formattedName = contact.get_formattedName();
                Sys.Debug.trace("Contact " + i + ": " + formattedName);
            }
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>Server-Side Data Filtering</h1>        
        <wl:signin onsignin="{{signInCallback}}"></wl:signin><br />       
    </div>

    <wl:app 
        channel-url="/channel.html" 
        callback-url="/OAuthWrapCallback.ashx?wl_session_id=<%= Session.SessionID %>"
        client-id="<Your-Client-Id>" 
        scope="WL_Contacts.View" 
        onload="appLoaded">
    </wl:app>
    <div style="position: fixed; bottom: 30px; left: 0px; right: 0px;">
        <textarea id="TraceConsole"  rows="15" cols="100" style="width: 99%"></textarea>
    </div>
    </form>
</body>
</html>

Show:
© 2014 Microsoft