How to: Return Lists
This programming 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.
Procedures
Before you begin, create a Windows application as described in Getting Started with Programmatically Customizing a SharePoint Web Site in Visual Studio. For information about setting a Web reference to a Windows SharePoint Services Web service, see Web Service Guidelines.
To add code to display the collection of lists
-
Open Form1 in Design view, open the Toolbox, and then drag a Label control and a Button control on to the form.
-
Right-click the Label1 control, click Properties, and then 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.
'Declare and initialize a variable for the Lists Web service. Dim myservice As New Web_Reference.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 'Set the Url property of the service for the path to a subsite. 'Not setting this property will return the lists in the root 'Web site sitelistService.Url = "http://Server_Name/Subsite_Name/_vti_bin/Lists.asmx" 'Declare an XmlNode object and initialize it with the XML 'response from the GetListCollection method. Dim node As System.Xml.XmlNode = myservice.GetListCollection() 'Loop through XML response and parse out the value of the 'Title attribute for each list. Dim xmlnode As System.Xml.XmlNode For Each xmlnode In node label1.Text += xmlnode.Attributes("Title").Value + ControlChars.Lf Next xmlnode
/*Declare and initialize a variable for the Lists Web service.*/ Web_Reference.Lists myservice = new Web_Reference.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; /*Set the Url property of the service for the path to a subsite. Not setting this property will return the lists in the root Web site.*/ listService.Url = "http://Server_Name/Subsite_Name/_vti_bin/Lists.asmx"; /*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.
- 8/19/2011
- Saju Vetiyatil
It might helpfull if you see more practical example. This example shows how to create browsing Document Library / List in Windows Application.
1. Create a new Windows Application project
2. Add reference to Microsoft Sharepoint Services
3. Create web reference to lists.asmx and name it ListsWS
3. Add TreeView Control, and name it treeDocumentLibrary;
4. Then this code can build up simple Document Library / List tree view.
private void Form1_Load(object sender, EventArgs e)
{
ListsWS.Lists listWS = new eDocWindows.ListsWS.Lists();
listWS.Credentials = System.Net.CredentialCache.DefaultCredentials;
listWS.Url = "
System.Xml.XmlNode xmlNode = listWS.GetListCollection();
TreeNode documentNode = new TreeNode("Document Library");
TreeNode listNode = new TreeNode("Lists");
foreach(System.Xml.XmlNode childNode in xmlNode)
{
if(childNode.Attributes["Hidden"].Value == "False")
{
if(childNode.Attributes["BaseType"].Value == "0")
listNode.Nodes.Add(childNode.Attributes["Title"].Value);
else
documentNode.Nodes.Add(childNode.Attributes["Title"].Value);
}
//treeDocumentLibrary.Nodes.Add(childNode.Attributes["Title"].Value);
}
treeDocumentLibrary.Nodes.Add(documentNode);
treeDocumentLibrary.Nodes.Add(listNode);
}