Thursday 30 May 2013

Accessing SharePoint 2010 Data with the Silverlight Client Object Model

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.

No comments:

Post a Comment