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.
- 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.
- 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.
- Launch Visual Studio 2010.
- In Visual Studio, start a new project by selecting File » New » Project.
- 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.
- Name the new project ListPrinter and click the OK button.
- 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:
Microsoft.SharePoint.Client.dll Microsoft.SharePoint.Client.Runtime.dll
- 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.
- Drag a ListBox control onto Form1. You’ll use this to show the lists available from the site. Set the Name property to ListsListBox.
- 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.
- Set the Form1Text property to Client Object Model.
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
- 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.
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;
Imports ClientOM = Microsoft.SharePoint.Client
- Enter the following code in the ShowButton_Click event to retrieve the available lists from the target site.
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; } }
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
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.
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
- Stop the project and return to Visual Studio.
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.