One common tasks that I have needed to do while working with SharePoint 2007 (MOSS) is to retrieve information in regards lists or libraries. You need this information when you want to perform add, update, or delete functions when working with data from a document library.
In today's post I will walk you though retrieving the list information. We will be getting this list information via the Lists.asmx web service.
Like I said above, this is a common task for me, so I have made this a utility style method. You will notice that I pass in 2 different parameters into this method. The first one is an enumeration value that contain the names of my different sites/sub-sites. The second one is the name of the document library that I an want to work with.
Code to pull out the list information:
/// <summary>
/// Will query for the list information via the correct web service for the given document site and name
/// </summary>
/// <param name="documentSite">The site enum to use</param>
/// <param name="documentLibraryName">The name of the library to hit. Cannot be null or empty</param>
/// <returns></returns>
public IListDataInformation GetListInformation( DocumentSite documentSite, string documentLibraryName )
{
if ( string.IsNullOrEmpty( documentLibraryName ) ) { throw new ArgumentException( "The document library provided was not valid." ); }
string baseUrl = "http://yourSiteNameHere";
string serviceDirectory = "_vti_bin";
string baseURL = string.Format("{0}/{1}/{2}", baseUrl, documentSite, serviceDirectory);
Lists listService = BuildListsService( baseURL );
XmlNode listResponse = listService.GetList( documentLibraryName );
string listID = listResponse.Attributes["ID"].Value; // Provides the GUID id for the actual list
string listVersion = listResponse.Attributes["Version"].Value; // Provides the version number for the actual ist
return new ListDataInformation( listID, documentLibraryName, listVersion );
}
Code below is called by the code above to build us a valid instance of the web service. This method here is public because it is actually part of another class. The Lists return type is an instance of the Webservice object.
public Lists BuildListsService(string baseURL)
{
if (string.IsNullOrEmpty(baseURL)) { throw new InvalidOperationException("The base URl was not populated correctly"); }
Lists returnService = new Lists();
// Here I create a valid credential to get around an issue we are having used the default credentails.
System.Net.ICredentials credential = new System.Net.NetworkCredential("UserName", "Password", "Domain");
returnService.Credentials = credential;
// We are not using this at this point in time because there is an issue with 401 error with
// some document libraries. Was not able to solve this so used the above for now.
//returnService.Credentials = System.Net.CredentialCache.DefaultCredentials;
returnService.Url = string.Format("{0}/{1}", baseURL, "Lists.asmx");
return returnService;
}
Code Explained
- First thing we need to do is build a connection to the web service
- Once we have our web service instance we need to connect to the service via the GetLists method. This will return an XmlNode object for use.
- After we have the XmlNode return value from the GetList method we need to pull out the ID and Version values. To take a look at all the possible values/data that is returned via the GetList method simply do a listResponse.OuterXml call on the listResponse XmlNode.
- Lastly we build an instance of my ListDataInformation object. This object is a simple object with 3 properties that hold the ID, name and version.
Here you have it, a pretty simple and straight forward way to retrieve different information about your Sharepoint List.
Till next time,

Posted
08-25-2007 8:42 PM
by
Derik Whittaker