
The Client Object Model
❘
405
query on the client context and execute it on the SharePoint server. The ExecuteQueryAsync
method executes all the queries that have been loaded onto the client context asynchro-
nously. Once the SharePoint server is fi nished, it calls back on one of the two callback meth-
ods passed depending on whether the request succeeded or failed. Add the follow code to the
ContactsViewModel.cs fi le to get the list data from SharePoint:
private void PopulateContacts()
{
//Get Data from SharePoint using the Client Object Model
//Get a reference to the current client context
ctx = ClientContext.Current;
//Specify the List and Listitems to return
List contactsList = ctx.Web.Lists.GetByTitle(“Contacts”);
contactsListItems =
contactsList.GetItems(CamlQuery.CreateAllItemsQuery());
//Add query to context and execute it on the server
ctx.Load(contactsListItems);
ctx.ExecuteQueryAsync(ContactsLoaded, ContactsLoadedFail);
}
Figure 14-16 shows what the Contacts list looks like in SharePoint. This is just a normal
SharePoint list that I created using sample data from the AdventureWorks database.
FIGURE 1416
584644c14.indd 405584644c14.indd 405 10/27/10 11:44:09 AM10/27/10 11:44:09 AM