Get all lists using Web Service in SharePoint 2010 Visual Web Part


In this article you will see how to get all the lists using a Web Service (_vti_bin/lists.asmx) and display the list title and description in a SharePoint 2010 Visual Web Part.

Create an Empty SharePoint Project using Visual Studio 2010:

1. Open Visual Studio 2010.
2. Go to File => New => Project.

WebSrvcShr1.gif

3. Select Empty SharePoint Project template from the installed templates.

WebSrvcShr2.gif

4. Enter the Name for the project as GetList and then click on Ok.
5. Select the local site that will be used for debugging and select "Deploy as a Farm Solution".

WebSrvcShr3.gif

6. Click on Finish.

Add Visual Web Part to the Empty SharePoint Project:


1. In the solution explorer, right click on Solution and click on Add.
2. Click on "New Item".

WebSrvcShr4.gif

3. Select Visual Web Part template from the installed templates.

WebSrvcShr5.gif

4. Enter the Name for the Visual Web Part as GetList and then click on Add.

Add Web Reference to the solution:

1. Right click on References folder and then click on "Add Service Reference".

WebSrvcShr6.gif

2. In the "Add Service Reference" wizard click on "Advanced..." button.

WebSrvcShr7.gif

3. In the "Service Reference Settings" wizard click on "Add Web Reference...".

WebSrvcShr8.gif

4. In the URL section, enter the lists web service URL and then click on enter.

WebSrvcShr9.gif

5. Enter the Web Reference Name and then click on "Add Reference" button.
6. Click on Ok.
7. Web Reference will be added successfully.

GetListUserControl.ascx:

1. Replace GetListUserControl.aspx with the following

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c"
%>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="GetListUserControl.ascx.cs" Inherits="GetList.GetList.GetListUserControl" %>
<asp:GridView ID="gvGetList" runat="server" CellPadding="4"
    EnableModelValidation="True" ForeColor="#333333" GridLines="None">
    <AlternatingRowStyle BackColor="White" />
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333"
/>
</asp:GridView>

GetListUserControl.ascx.cs:

1. Add the following Namespaces

  1. using GetList.ListsWebReference;
  2. using System.Net;
  3. using System.Data;

2. Replace GetListUserControl.aspx.cs with the following code

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using GetList.ListsWebReference;
using System.Net;
using System.Data;
namespace GetList.GetList
{
    public partial class GetListUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataSet dataSet;
            DataTable dataTable;
            DataColumn dataColumn;
            DataRow dataRow;
            Lists listReference = new Lists();
            listReference.Url = "http://serverName:10736/sites/ECT/_vti_bin/lists.asmx";
            listReference.Credentials = CredentialCache.DefaultCredentials;
            System.Xml.XmlNode node = listReference.GetListCollection();
            dataTable = new DataTable();
            dataColumn = new DataColumn("Title");
            dataTable.Columns.Add(dataColumn);
            dataColumn = new DataColumn("Description");
            dataTable.Columns.Add(dataColumn);
            dataSet = new DataSet();
            dataSet.Tables.Add(dataTable);
            foreach (System.Xml.XmlNode xmlnode in node)
            {
                dataRow = dataTable.NewRow();
                dataRow["Title"] = xmlnode.Attributes["Title"].Value;
                dataRow["Description"] = xmlnode.Attributes["Description"].Value;
                dataTable.Rows.Add(dataRow);
            }
            gvGetList.DataSource = dataSet;
            gvGetList.DataBind();
        }
    }
}



Deploy the solution:

  1. Build the solution
  2. Hit F5.

Create SharePoint 2010 Visual Web Part in the SharePoint site:

1. Open the SharePoint Site.
2. Go to Site Actions => Edit the page.
3. Go to Editing Tools in the ribbon interface => Insert => Web Part.
4. In the Categories section, Select Custom and then select GetList.

WebSrvcShr10.gif

5. Click on Add.
6. Visual Web Part displays all the list title and description as shown in the following

WebSrvcShr11.gif

Summary:

Thus in this article you have seen how to get all the lists using Web Service (_vti_bin/lists.asmx) and display the list title and description in SharePoint 2010 Visual Web Part.

Up Next
    Ebook Download
    View all
    Learn
    View all