Procedimiento para devolver listas
Esta tarea de programación muestra cómo crear un sencillo formulario Windows Forms que usa el método GetListCollection del servicio web Lists para recuperar la colección de listas y mostrar sus nombres.
Procedimientos
Antes de empezar, cree una aplicación de Windows como se describe en Introducción a la personalización mediante programación de un sitio web de SharePoint en Visual Studio. Para obtener información acerca de cómo establecer una referencia web a un servicio web de Windows SharePoint Services, consulte Instrucciones para los servicios web.
Para agregar código que muestre la colección de listas
Abra Form1 en la vista Diseño, abra el Cuadro de herramientas y, a continuación, arrastre un control Label y un control Button hasta el formulario.
Haga clic con el botón secundario en el control Label1, haga clic en Propiedades y, a continuación, elimine el valor Label1 de la propiedad Text del control.
Haga doble clic en el control Button1 para mostrar el editor de código y, a continuación, agregue las siguientes líneas de código al controlador del evento Click.
'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"; }
En el menú Depurar, haga clic en Iniciar para probar el formulario. Haga clic en Button1 para mostrar las listas en el sitio de SharePoint.