Does the SP List Exist?
The SharePoint Lists web service is very useful and method rich for working with SharePoint lists, however one issue I ran in to recently left me wanting.
I had a series of tasks to code and one was to determine if a SharePoint list exists in one primary default location and if it did use that list but if it did not I would bind to another list in a backup location.
The Lists web service does not have an Exists method so I created this function below to do the job for me.
1: private static bool ListExists(string webUrl, string list)
2: {
3: try
4: {
5: Lists lists = new Lists();
6: lists.Credentials = CredentialCache.DefaultCredentials;
7: lists.PreAuthenticate = true;
8: if (webUrl.EndsWith("/"))
9: lists.Url = webUrl + "_vti_bin/lists.asmx";
10: else
11: lists.Url = webUrl + "/_vti_bin/lists.asmx";
12: try
13: {
14: XmlNode xmlList = lists.GetList(list);
15: return true;
16: }
17: catch (Exception ex)
18: {
19: Trace.WriteLine(ex.Message + " " + ex.StackTrace);
20: return false;
21: }
22: }
23: catch (Exception ex)
24: {
25: Trace.WriteLine(ex.Message + " " + ex.StackTrace);
26: return false;
27: }
28: }
If GetLists works then xmlList object will contain a XML scheme document, list exists. If the list does not exist a SOAP exception will be trapped and the ListExists method returns false.
For more information see: Lists.GetList Method
