Building WPF Applications by Using the SharePoint 2010 Client Object Model
Getting Started with Web Development in SharePoint 2010: Learn how to create a Windows Presentation Foundation (WPF) application that uses the SharePoint 2010 client object model to retrieve data from a SharePoint Web site.
Last modified: January 27, 2011
Applies to: Office 2010 | SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2010
Published: April 2010
Provided by: Frank Rice, Microsoft Corporation
The SharePoint 2010 client object model provides a unified and complete object model for Microsoft SharePoint 2010 developers to access SharePoint site data from remote clients. In this exercise, you use the new SharePoint 2010 client object model and create a Windows Presentation Foundation (WPF) application to retrieve list data from a SharePoint site. To complete this task, you must do the following:
In this task, you create a WPF application in Microsoft Visual Studio 2010.
To create the WPF application
To start Visual Studio 2010, click the Start Menu, click All Programs, click Microsoft Visual Studio 2010, and then click Microsoft Visual Studio 2010.
On the File menu, point to New, and then click Project.
In the New Project dialog window, in the Installed Templates section, click Visual C#, and then click Windows.
Select WPF Application from the project items.
In the Name box, type CreateWPFAppUsingClientOM and then click OK.
In Solution Explorer, right-click CreateWPFAppUsingClientOM and then click Properties.
In the Application tab, change the Target Framework property to .NET Framework 4.0.
In Solution Explorer, right-click References and then click Add Reference.Switch to the Browse tab.
Type the following path in the File Name box and press Enter:
C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI
Select Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll, and then click OK.
In this task, you add a ListBox control to the WPF grid view and then add code to add functionality to the control.
To create the user interface
In Solution Explorer, double-click MainWindow.xaml to open the XAML view.
Insert the following XAML code inside the Grid element.
This XAML code creates a new ListBox control that you should see in Design view, as shown in Figure 1.
In Solution Explorer, right-click MainWindow.xaml and then click View Code.
Add the following using statements after the existing using statements.
Insert the following code in the MainWindow constructor after the InitializeComponent() method. Be sure to change the Web site URL to your own Web site.
ClientContext context = new ClientContext("http://intranet.contoso.com"); Web site = context.Web; context.Load(site, osite => osite.Title); context.ExecuteQuery(); Title = site.Title; ListCollection lists = site.Lists; IEnumerable<SP.List> listsCollection = context.LoadQuery(lists.Include(l => l.Title, l => l.Id)); context.ExecuteQuery(); ListBox1.ItemsSource = listsCollection; ListBox1.DisplayMemberPath = "Title";
This code does the following:
Creates a client context to the SharePoint site.
Queries the available SharePoint lists in the site and retrieves the lists together with their Title and Id properties.
Binds the list collection to the WPF ListBox control.