Accessing SharePoint 2010 Data with the Silverlight Client Object Model
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
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.
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.
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.
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.
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.
> [!VIDEO https://www.microsoft.com/en-us/videoplayer/embed/6ec4256a-c12c-462f-b6c9-77ab9b0d3395]
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.