This code will give you all the list from SharePoint with the details of particular list. You can change BaseType to get different types of list from SharePoint.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Resources;
using System.Web;
//To add below namespace you should add below reference library.
//1.)Microsoft.SharePoint.Client.dll
//2.)Microsoft.SharePoint.Client.Runtime.dll
using Microsoft.SharePoint; //add this namespace to access SharePoint Web Service.
using Microsoft.SharePoint.Client; //add this namespace to access SharePoint Web Service.
using Microsoft.SharePoint.Utilities; //add this namespace to access SharePoint Web Service.
namespace WindowsFormsApplication1
{
public partial class COM : System.Windows.Forms.Form
{
string url = string.Empty;
string listName = string.Empty;
string listGUid = string.Empty;
string rowLimit = "";
System.Net.NetworkCredential creadential = new System.Net.NetworkCredential("username", "password", "domain");
public COM()
{
InitializeComponent();
}
private void btngetlist_Click(object sender, EventArgs e)
{
string listname = string.Empty;
if (txturl.Text == string.Empty)
{
MessageBox.Show("Please Insert Valid Site URL!", "URL Needed!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
url = txturl.Text;
List<string> lst = getlist(url);
}
}
public List<string> getlist(string url)
{
string listname = string.Empty;
ListWebService.Lists lst = new ListWebService.Lists();
List<string> listnames = new List<string>();
lst.Url = url + "/_vti_bin/Lists.asmx";
lst.Credentials = creadential;
try
{
XmlNode xnode = lst.GetListCollection();
XmlNodeList node = xnode.SelectNodes("*[@BaseType=1]"); //change BaseType to retrieve Different list.
foreach (XmlNode list in node)
{
//if(Convert.ToInt32(list.Attributes["BaseType"]) == 0)
listname = list.Attributes["Title"].Value.ToString();
combolist.Items.Add(listname);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return listnames;
}
public void combolist_SelectedIndexChanged(object sender, EventArgs e)
{
ListWebService.Lists lst = new ListWebService.Lists();
url = txturl.Text;
lst.Credentials = creadential;
try
{
listName = combolist.SelectedItem.ToString();
XmlNode xnode = lst.GetListCollection();
XmlNode list = lst.GetList(listName);
XmlNodeList visibleColumns= list.SelectNodes("Fields/Field[not(@Hidden) or @Hidden='FALSE']");
System.Xml.XmlNode nodlistview = lst.GetListAndView(listName, string.Empty);
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(nodlistview.OuterXml);
XmlNodeList xmllist = xmldoc.GetElementsByTagName("ViewFields");
xmldoc = new System.Xml.XmlDocument();
System.Xml.XmlElement query = xmldoc.CreateElement("Query");
System.Xml.XmlElement viewfield = xmldoc.CreateElement("ViewFields");
xnode = xmllist.Item(0);
string queryinnerxml = string.Empty;
for (int i = 0; i < xnode.ChildNodes.Count; i++)
{
if (xnode.ChildNodes[i].Attributes["Name"].Value != "Edit")
{
queryinnerxml = queryinnerxml + "<FieldRef Name=\"" + xnode.ChildNodes[i].Attributes["Name"].Value + "\" />";
}
}
viewfield.InnerXml = queryinnerxml.Trim();
System.Xml.XmlElement queryoption = xmldoc.CreateElement("QueryOptions");
queryoption.InnerXml = "<IncludeMandatoryColumns>TRUE</IncludeMandatoryColumns>";
System.Xml.XmlNode nodelistitem = lst.GetListItems(listName, string.Empty, query, viewfield, rowLimit, queryoption, null);
System.IO.StringReader xmlsr = new System.IO.StringReader(nodelistitem.InnerXml);
DataSet ds = new DataSet();
ds.ReadXml(xmlsr);
listgrid.DataSource = ds.Tables[1];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}