Share via


Exercise 1: Retrieving Lists

In this exercise you will create the first part of a Windows application for printing SharePoint lists as Word documents. You will retrieve the available lists from a SharePoint site using the .NET Client Object model.

  1. If you haven’t already done so, run the batch file named SetupLab06.bat, found in the c:\Student\Labs\06_ClientOM\ folder, to create the new site collection that will be used to test and debug the code you will be writing in this lab. This batch file creates a new site collection at an URL of https://intranet.contoso.com/sites/Lab06.
  2. Launch Internet Explorer and navigate to the top-level site at https://intranet.contoso.com/sites/Lab06. Take a moment to inspect the site and make sure it behaves as expected. Note that the setup script creates a new site collection with a Blank site as its top-level site.
  3. Launch Visual Studio 2010.
  4. In Visual Studio, start a new project by selecting File » New » Project.
  5. In the New Project dialog, expand the nodes Visual C#/Visual Basic » Windows and select Windows Forms Application. Make sure that Framework 3.5 is selected.
  6. Name the new project ListPrinter and click the OK button.
  7. Add a reference to the Client Object Model by selecting Project » Add Reference from the Visual Studio main menu. In the References dialog, on the .NET tab select both the Microsoft.SharePoint.Client and Micorosoft.SharePoint.Client.Runtime compontents. Click OK.Note: if you cannot find these in the .Net tab, Browse to C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\ISAPI and select the following assemblies, then click OK to add the references to the project:

    C#

    Microsoft.SharePoint.Client.dll Microsoft.SharePoint.Client.Runtime.dll
  8. Drag a TextBox control onto Form1. You’ll use this TextBox to specify the Url for the site where you want to access list data. Set the Name property to UrlTextBox.
  9. Drag a ListBox control onto Form1. You’ll use this to show the lists available from the site. Set the Name property to ListsListBox.
  10. Drag a Button control onto Form1. You’ll use this button to connect with the site and fill the ListBox. Change the Text property of this button to Show Lists and the Name property to ShowButton.
  11. Set the Form1Text property to Client Object Model.
  12. You can optionally drag additional labels onto the form to make it look like the following:

    Figure 1

    Design the user interface of the form

  13. Double-click the Button control to open the code window. To avoid ambiguous references between the Client Object Model and the Windows Forms namespace, add the following statement to the top of the code module.

    C#

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;using ClientOM = Microsoft.SharePoint.Client;

    VB.NET

    Imports ClientOM = Microsoft.SharePoint.Client
  14. Enter the following code in the ShowButton_Click event to retrieve the available lists from the target site.

    C#

    private void ShowButton_Click(object sender, EventArgs e) { //Show the hourglass wait cursor this.Cursor = Cursors.WaitCursor; ListsListBox.Items.Clear(); //Get a context using (ClientOM.ClientContext ctx = new ClientOM.ClientContext(UrlTextBox.Text)) { //Get the site ClientOM.Web site = ctx.Web; ctx.Load(site); //Get Lists ctx.Load(site.Lists); //Query ctx.ExecuteQuery(); //Fill List foreach (ClientOM.List list in site.Lists) { ListsListBox.Items.Add(list.Title); } //Return the cursor to normal this.Cursor = Cursors.Default; } }

    VB.NET

    Private Sub ShowButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ShowButton.Click 'Show the hourglass wait cursor Me.Cursor = Cursors.WaitCursor ListsListBox.Items.Clear() 'Get a context Using ctx As New ClientOM.ClientContext(UrlTextBox.Text) 'Get the site Dim site As ClientOM.Web = ctx.Web ctx.Load(site) 'Get Lists ctx.Load(site.Lists) 'Query ctx.ExecuteQuery() 'Fill List For Each list As ClientOM.List In site.Lists ListsListBox.Items.Add(list.Title) Next 'Return the cursor to normal Me.Cursor = Cursors.[Default] End Using End Sub
  15. Run your project, enter https://intranet.contoso.com/sites/Lab06 in the TextBox control and click the Show Lists button. Verify that you are returning lists from the site.

    Note:
    If you get an error when the ClientContext calls ExecuteQuery(), try navigating to the site in the browser first, then rerun the code.

    Figure 2

    The .NET Client object model in action

  16. Stop the project and return to Visual Studio.
    Note:
    In this exercise you created a Windows Forms application that used the SharePoint Client Object Model to query a SharePoint site for all the lists in the site and display them in a listbox.