Returning Lists
This programing task shows how to create a simple Windows Form that uses the GetListCollection method of the Lists Web service to retrieve the collection of lists and display their names.
To create a new Window Application project
- Start Microsoft Visual Studio .NET.
- On the File menu, point to New, and then click Project.
- In the New Project dialog box, click Visual C# Projects, and then select the Windows Application template.
- Type "ShowLists" as the name and specify the location for the project files, and then click OK.
To add a reference to the Lists Web Service
- In Solution Explorer, right-click Web References, and then click Add Web Reference.
- In the Add Web Reference dialog box, enter the URL to the Lists Web service on the server running Microsoft Windows SharePoint Services:
http://servername/_vti_bin/lists.asmx
and then click Add Reference.
To add code to display the collection of lists
Open Form1 in Design view, display the Toolbox, and then drag a Label control and a Button control on to the form.
Right-click the Label1 control, and then click Properties and delete the value Label1 from the Text property of the control.
Double-click the Button1 control to display the Code Editor, and add the following lines of code to the Click event handler:
private void button1_Click(object sender, System.EventArgs e) { // Declare and initialize a variable for the Lists Web Service. servername.Lists myservice = new servername.Lists(); // Authenticate the current user by passing their default // credentials to the Web Service from the system credential cache. myservice.Credentials = System.Net.CredentialCache.DefaultCredentials; // Declare an XmlNode object and initialize it with the XML // response from the GetListCollection method. System.Xml.XmlNode node = myservice.GetListCollection(); // Loop through XML response and parse out the value of the // Title attribute for each list. foreach(System.Xml.XmlNode xmlnode in node) { label1.Text+=xmlnode.Attributes["Title"].Value + "\n"; } }
On the Debug menu, click Start to test the form.
Click Button1 to display the lists in your SharePoint site.