Starting Out: Programming SharePoint Services
Many functions that the SharePoint web interface does you can do through calling web services provided by SharePoint – this is thanks to the SOA application architecture. Here’s a starting guide to get you going. This example uses a WinForm application but the same techniques can be used with ASP.NET applications. This program gets the names and GUIDs of all lists in the site (but not sub-sites) and displays the names in a list box. When the user selects a list name the GUID for the list will be displayed:

First, you need to create a web reference to the SharePoint web service. The URL for the web reference looks like:
http://server:port/sites/intranet/projectx/_vti_bin/lists.asmx
The URL includes the server and port, and the path to the site which you want to program against – “projectx” in this case.
Now the coding starts. First, create an instance of the web service (the proxy name is “moss2007″ in this case) for the “Lists” SharePoint web service. Then set the credentials used to authenticate against the service. In the WinForm example this is the currently logged on user:
moss2007.Lists lst = new moss2007.Lists();
lst.Credentials = System.Net.CredentialCache.DefaultCredentials;
Now a call can be made to get the “List” – this is returned as an XmlNode list:
XmlNode nl = lst.GetListCollection();
In this example, the list is added to a list box. Each List in SharePoint has an “ID” which is a GUID, and when using lists through web services you should use the “ID” to refer to a list and not its name. Therefore, the list of names is going to be added to a list box and the GUIDs stored against them. Then, when the user selects a list the list can be accessed through the GUID. To do this, a class is created to manage items in the list box:
public class LstDataItem
{
public LstDataItem(string sDisplay, Guid gGuid)
{
display = sDisplay;
theGuid = gGuid;
}
private String display;
public String Display
{
get { return display; }
set { display = value; }
}
private Guid theGuid;
public Guid TheGuid
{
get { return theGuid; }
set { theGuid = value; }
}
public override string ToString()
{
return Display;
}
}
The list can now be loaded into the list box:
foreach (XmlNode nd in nl.ChildNodes)
{
lstLists.Items.Add(new LstDataItem(nd.Attributes["Title"].Value,
new Guid(nd.Attributes["ID"].Value)));
}
This code iterates over the list of nodes, and extracts the name from the “Title” attribute and the GUID from the “ID” attribute. These are added to the list box Items collection. Finally, an event handler is created to respond to the user selecting an item from the list box:
private void lstLists_SelectedIndexChanged(object sender, EventArgs e)
{
LstDataItem ldi = (LstDataItem)lstLists.SelectedItem;
if (ldi != null)
{
MessageBox.Show(ldi.TheGuid.ToString());
}
}
Here is the complete code for loading the list in the Form’s Load event. It includes exception handling for errors thrown by the SharePoint web service:
private void Form1_Load(object sender, EventArgs e)
{
try
{
moss2007.Lists lst = new moss2007.Lists();
lst.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlNode nl = lst.GetListCollection();
foreach (XmlNode nd in nl.ChildNodes)
{
lstLists.Items.Add(new LstDataItem(nd.Attributes["Title"].Value,
new Guid(nd.Attributes["ID"].Value)));
}
}
catch (System.Net.WebException wex)
{
MessageBox.Show(wex.Message);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
MessageBox.Show(“Message:\n” + ex.Message + “\nDetail:\n” +
ex.Detail.InnerText + “\nStackTrace:\n” + ex.StackTrace);
}
}
[...] SharePoint List Web Service Calls Published July 14th, 2007 SharePoint In this post I showed how the SharePoint Web Services can be used to get a list of the lists in a [...]
More SharePoint List Web Service Calls « Nick Grattan’s Techy Blog
July 27, 2007 at 11:48 am
Hi. I am using a bit of VB 2005 code which extracts SharePoint 2003 List names and displays them in a list box. However when I use SharePoint 2007 the same code extracts the list names as GUIDS. How do I display just the list names in SharePoint 2007? Any help appreciated. Thanks.
Rudy
September 12, 2007 at 12:40 pm
In the above code, the name of the list is obtained through the nd.Attributes["Title"].Value call. The GUID is returned through the “ID” attribute. So, to get the list name use the “Title” attribute. Hope this helps, Nick.
nickgrattan
September 12, 2007 at 1:56 pm
Can you give me a example of using nd.Attributes[”Title”].Value in Visual Studio 2005 VB code?
Rudy
November 2, 2007 at 3:11 pm
Thanks for posting this, I was wondering if you knew how to authenticate from your code when using a hosted SharePoint site. Everything else seems straight forward except for trying to connect when you are not on the same network.
Thanks
Jason
November 14, 2007 at 5:35 pm
To authenticate onto another server, add a “using System.Net;” and then use the following code to create a NetworkCredential object:
lst = new moss2007.Lists();
NetworkCredential nc = new NetworkCredential();
nc.UserName = “Administrator”;
nc.Password = “password”;
lst.Credentials = nc;
(That’s not my password, honest!) Nick.
nickgrattan
November 16, 2007 at 2:23 pm
Here’s the VB sample for accessing the Attributes collection:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim lst As New moss2007.Lists
lst.Credentials = System.Net.CredentialCache.DefaultCredentials
Dim nl As XmlNode = lst.GetListCollection()
For Each nd As XmlNode In nl.ChildNodes
lstLists.Items.Add(New LstDataItem(nd.Attributes(“Title”).Value, New Guid(nd.Attributes(“ID”).Value)))
Next
End Sub
You can use http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx for automating the convertion. Sorry for the delay. Nick.
nickgrattan
November 16, 2007 at 2:37 pm
hi,
How to add a list based on the template that I created manually in the site, using lists web service ?
sanju
November 29, 2007 at 12:14 pm
Hi,
How can get the each item in the list?
Thanking in Advance
Haf
October 22, 2008 at 9:24 am