Accessing SharePoint 2010 Data with the Silverlight Client Object Model

SharePoint Visual How To

Summary:  Learn how to use the Silverlight client object model in Microsoft SharePoint 2010 to read and write data in SharePoint sites from Microsoft Silverlight applications.

Applies to: SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2008 | Visual Studio 2010

Provided by:  Andrew Connell, MVP, Critical Path Training

Overview

With each release of Microsoft SharePoint, developers ask for additional web services to simplify reading and writing SharePoint site data from custom code that is not on the server that is running SharePoint. Instead of building more and more web services, Microsoft introduced in SharePoint 2010 the client object model, which contains a familiar subset of frequently used objects. The client object model is very similar to the SharePoint server-side object model. This SharePoint Visual How To demonstrates how to use the SharePoint 2010 Silverlight client object model.

Code It

In your project, add references to the two required assemblies that compose the Silverlight client object model. These two assembles, Microsoft.SharePoint.Client.Silverlight.dll and Microsoft.SharePoint.Client.Silverlight.Runtime.dll, are located in the path %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\LAYOUTS\ClientBin.

Connect to SharePoint and get some data. The example in the video demonstrates how to get a list of product categories from a SharePoint list. All communication centers around the ClientContext object. This object connects to the server and sends all queued commands for processing.

After you use either the ClientContext.Load method or the ClientContext.LoadQuery method to queue instructions, a call to the ClientContext.ExecuteQuery method triggers the communication back to the server, as shown in the following example.

SynchronizationContext _syncContext = 
  SynchronizationContext.Current; 
ClientContext  _clientContext = 
  new ClientContext("http://intranet.wingtip.com");
[..]
List categoryList = 
  _clientContext.Web.Lists.GetByTitle("Product Categories");

CamlQuery query = new CamlQuery();
query.ViewXml = 
  "<View><Query><OrderBy><FieldRef Name='Title' /></OrderBy></Query></View>";
ListItemCollection _productCategories = 
  categoryList.GetItems(query);

_clientContext.Load(_productCategories);
_clientContext.ExecuteQueryAsync(OnSucceedListenerGetCategories,
  OnFailListener);

The successful callback method is called asynchronously and passes a delegate back to the main thread to update the user interface, as shown in the following example.

private void OnSucceedListenerGetCategories(object sender, 
  ClientRequestEventArgs args)
{
    _syncContext.Post(OnSucceedGetCategories, null);
}
private void OnSucceedGetCategories(object sender)
{
    if (_productCategories!= null)
        ProductCategoryListBox.ItemsSource = _productCategories;
}

Adding Items to SharePoint Lists

Another useful technique is to add items to SharePoint lists. You do this by using creation information objects. For list items, the creation information object is the ListItemCreationInformation object. By using this object, you can create a list item, update the fields of the new list item, and save the changes, as shown in the following example.

List products = 
  _clientContext.Web.Lists.GetByTitle("Products");
ListItemCreationInformation newProductInfo = 
  new ListItemCreationInformation();

ListItem newProduct = products.AddItem(newProductInfo);
// "dialog" is a child window that displays a form 
// with a few controls.
newProduct["Title"] = dialog.ProductNameTextBox.Text;
newProduct["Product_x0020_Number"] = 
  dialog.ProductNumberTextBox.Text;
newProduct["Price"] = dialog.ProductPriceTextBox.Text;
FieldLookupValue fieldValue = new FieldLookupValue();
foreach (ListItem item in Global.ProductCategories)
    if (item["Title"].ToString() == 
      dialog.ProductCategortyComboBox.SelectedItem.ToString())
    {
        fieldValue.LookupId = item.Id;
    }
newProduct["Category"] = fieldValue;

newProduct.Update();
_clientContext.ExecuteQueryAsync(OnSucceededListenerCreateProduct,
  OnFailListener);
Read It

The client object model in SharePoint 2010 simplifies interacting programmatically with SharePoint sites from custom code that is not running on the same server as SharePoint 2010. This Visual How To demonstrates how to use the Silverlight client object model to create a rich web application that communicates directly with SharePoint. Instead of using the traditional web services approach, the client object model provides a rich, SharePoint-specific object model that closely matches the SharePoint 2010 server object model.

See It

Watch the video

> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/6ec4256a-c12c-462f-b6c9-77ab9b0d3395]

Length: 00:15:16

Click to grab code

Grab the Code

Explore It

About the Author

MVP Andrew Connell is an author, instructor, and co-founder of Critical Path Training, a SharePoint education-focused company. Andrew is a six-time recipient of Microsoft’s Most Valuable Professional (MVP) award (2005-2010) for Microsoft Content Management Server (MCMS) and Microsoft SharePoint Server. He authored and contributed to numerous MCMS and SharePoint books over the years including his book Professional SharePoint 2007 Web Content Management Development by WROX. Andrew speaks about SharePoint development and WCM at conferences such as Tech-Ed, SharePoint Connections, VSLive, SharePoint Best Practice Conference, SharePoint Evolutions Conference, Office Developer Conference, and Microsoft SharePoint Conference in the United States, Australia, England, and Spain. Andrew blogs at Andrew Connell Blog and on Twitter @andrewconnell.