Saturday, March 29, 2008

Working with SharePoint list web services

SharePoint lists can be very useful to increase collaboration from within applications across the enterprise. SharePoint exposes web services that can be used to get information from the lists and add/update information in the lists. I have provided a sample of code below that gets the collection of lists on a site. You can then use this to find the internal name of a list. This value will be needed to interact with other services pertaining to the specific list.

In the sample below, I compared the title of the list to the “Title” attribute which is the generic display name. The “Name” attribute is the internal name of the list.

:: begin code sample ::
:: listName will be equal to the GUID of the list corresponding to the specified name ::

string listName;
XmlNode col = null;
XmlDocument doc = null;
XmlElement elem = null;

//setup the service
_service = new wsLists.Lists();
_service.Url = _siteUrl + "/_vti_bin/Lists.asmx";

//get the collection of lists from the service
col = _service.GetListCollection();
//load the collection into a document
doc = new XmlDocument();
doc.LoadXml(col.OuterXml.Trim());
//the document will have one child node that will contain all children
if ((doc.HasChildNodes) && (doc.ChildNodes.Item(0).HasChildNodes))
{
//look through the children to find the desired node
foreach (XmlNode node in doc.ChildNodes.Item(0).ChildNodes)
{
if (node.NodeType == XmlNodeType.Element)
{
elem = (XmlElement)node;
if (elem.GetAttribute("Title") == "My List Name")
{
listName = elem.GetAttribute("Name");
break;
}
}
}
}

:: end code sample ::

Future blog(s):
  1. Part 2 of this entry discussing how to get a collection of the items in the list.

1 comment:

David Heeter said...

Thanks for the great post! SharePoint Web Services are awesome!