Share via


Building WPF Applications by Using the SharePoint 2010 Client Object Model

SharePoint QuickStart Banner

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.

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

In this article
Create a WPF Application
Create the User Interface
Use the Client Object Model to Retrieve the List Data
Next Steps

Published:  April 2010

Provided by:   Frank Rice, Microsoft Corporation

Click to view video   Watch the Video

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:

  • Create a WPF Application

  • Create the User Interface

  • Use the Client Object Model to Retrieve the List Data

Create a WPF Application

In this task, you create a WPF application in Microsoft Visual Studio 2010.

To create the WPF application

  1. To start Visual Studio 2010, click the Start Menu, click All Programs, click Microsoft Visual Studio 2010, and then click Microsoft Visual Studio 2010.

  2. On the File menu, point to New, and then click Project.

  3. In the New Project dialog window, in the Installed Templates section, click Visual C#, and then click Windows.

  4. Select WPF Application from the project items.

  5. In the Name box, type CreateWPFAppUsingClientOM and then click OK.

  6. In Solution Explorer, right-click CreateWPFAppUsingClientOM and then click Properties.

  7. In the Application tab, change the Target Framework property to .NET Framework 4.0.

  8. In Solution Explorer, right-click References and then click Add Reference.Switch to the Browse tab.

  9. Type the following path in the File Name box and press Enter:

    C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI

  10. Select Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll, and then click OK.

Create the User Interface

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

  1. In Solution Explorer, double-click MainWindow.xaml to open the XAML view.

  2. Insert the following XAML code inside the Grid element.

    <ListBox Height="238" Margin="12,11,12,13" Name="ListBox1" Width="254" />
    

    This XAML code creates a new ListBox control that you should see in Design view, as shown in Figure 1.

    Figure 1. ListBox in Design view

    LiistBox in Designer view

  3. In Solution Explorer, right-click MainWindow.xaml and then click View Code.

  4. Add the following using statements after the existing using statements.

    using Microsoft.SharePoint.Client;
    using SP = Microsoft.SharePoint.Client;
    
  5. 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.

Use the Client Object Model to Retrieve the List Data

In this task, you run the application and observe the results.

To test the project

  1. Press F5 to start the application.

    You should see the application window with the ListBox control now populated with SharePoint lists, as shown in Figure 2.

    Figure 2. ListBox control including SharePoint lists

    ListBox control with the listing of lists

  2. Close the application window.

Next Steps