Procedimiento para devolver listas

Última modificación: martes, 03 de agosto de 2010

Hace referencia a: SharePoint Foundation 2010

Esta tarea de programación muestra cómo crear un formulario Windows Forms sencillo, 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 Forms en Microsoft Visual Studio. Para obtener información acerca de cómo establecer una referencia web a un servicio web de Microsoft SharePoint Foundation, vea Instrucciones para los servicios web.

Para agregar código que muestre la colección de listas

  1. Abra Form1 en la vista Diseño, abra el Cuadro de herramientas y, a continuación, arrastre un control de botón y un control de cuadro de texto al formulario.

  2. Redimensione el cuadro de texto para rellenar el formulario debajo del botón.

  3. Haga clic con el botón secundario en el control de cuadro de texto, haga clic en Propiedades y, a continuación, establezca la propiedad Multiline en True y la propiedad ScrollBars en Vertical.

  4. Haga doble clic en el control Button para mostrar el Editor de código y, a continuación, agregue las siguientes líneas de código al controlador de eventos Button1_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
    myservice.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
       textBox1.Text += xmlnode.Attributes("Title").Value + Environment.NewLine
    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.*/
    myservice.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) 
    {
       textBox1.Text+=xmlnode.Attributes["Title"].Value + Environment.NewLine;
    }
    
  5. En el menú Depurar, haga clic en Iniciar depuración para probar el formulario. Haga clic en el botón del formulario para mostrar las listas del sitio de SharePoint.