Geeks With Blogs
Fringe SharePoint Continued
During my project that is heavily centered around web services, I often found that before I could execute one overall integration, I would need about 3 or 4 web Service calls to collect all the parameters I needed to actually call the web service call I was really trying to execute. I came across an interesting situation where I need to retrieve the List from SharePoint. A web service call will send back an XMLElement with all the information available for a list. The problem is that the list needs to exist for a web service call to succesfully execute. If the list does not exist then it will return an exception.

This is more along the lines of what I was implementing: (the object '_service' is the the instantiated ListSoapClient that is available through WCF to make my calls)

 private XmlElement GetListWSCall(string listName)
        {
           XmlElement retval = null;
            try
            {
                retval =  _service.GetList(listName);
               
            }
            catch (Exception ex)
            {
                retval = null;
            }
            return retval;
        }

The problem with this is that if the Library does not exist it will be handled by an exception. Bill Simser wrote a good article on this already, so please read his blog post about this issue. What I would like to point out is my solution for the web service call not the object model he worked with. Same concept though.

I wrote a method as well that would check if the List exited in SharePoint. I simply got all the lists for a given endpoint and then used Linq to get at the document library name. It looks like this:

 public bool DocLibExists(string listName)
        {
            try
            {
                var allLists = _service.GetListCollection();
               
               return allLists.ChildNodes.OfType<XmlElement>().ToList().Exists(x => x.Attributes["Title"].Value ==listName);
            }
            catch (Exception ex)
            {
              //handle whatever connection error you might get form the web service call.
            }
            return false;
        }

I have really replaced all of my "foreach" statements and even the typical Linq statments by this somewhat daisy chain of linq query calls. It makes for tighter code as long as the daisy chain does not get huge.  Your result set you get back is really an XmlElement with child nodes that are represent a list in SharePoint. All you are really after in this scenario is the "Attributes". When you debug and inspect the XmlElement you can take a look at all the Attributes and adjust your code accordingly to get what you are really after.


That's all for now, More to come!

Juan Posted on Wednesday, August 19, 2009 5:39 PM SharePoint | Back to top


Comments on this post: Extending SharePoint: Checking if a List exists (Web Services)

# re: Extending SharePoint: Checking if a List exists (Web Services)
Requesting Gravatar...
That's another nice way to check. But I prefer not to use LINQ because it can help your code run on .NET 2.0 :-)
Left by Duy Lam on Aug 20, 2009 6:20 AM

# re: Extending SharePoint: Checking if a List exists (Web Services)
Requesting Gravatar...
It's a good idea to always use a finally{} for disposing your try/catch.
Left by Brendan on Aug 24, 2009 8:35 AM

Your comment:
 (will show your gravatar)


Copyright © juanlarios | Powered by: GeeksWithBlogs.net