in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint, Office 365, Mobile Development, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, Office 365 / SharePoint Online and Visual Studio 2013.

Checking to see if a list exists in SharePoint 2010

This is a bitter subject with most MOSS 2007 developers because the most common way to do this is by using a try/catch block when you try to use the indexer on SPListCollection.  Well, I am pleased to tell you about a new method I discovered on SPListCollection that really made my day.  The new TryGetList method takes the name of a list and will get this, return a null if the list doesn’t exist.  Take a look at this code.

using (SPSite siteCollection = new SPSite("http://moss-server"))

{

    SPList myCustomList = siteCollection.RootWeb.Lists.TryGetList("MyCustomList");

 

    // doesn't throw exception!

    if (myCustomList != null)

    {

        // do something

    }

}

That’s right!  You heard me, instead of throwing an exception, you actually get null back when the list doesn’t exist.  No more try/catch blocks.  No more extension methods to hide those try/catch blocks.  Unfortunately, there is nothing on SPBaseCollection to check if an item exists, but I do have a solution, so be looking for that post.

Comments

 

Twitter Trackbacks for Checking to see if a list exists in SharePoint 2010 - Corey Roth - DotNetMafia.com - Tip of the Day [dotnetmafia.com] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Checking to see if a list exists in SharePoint 2010 - Corey Roth - DotNetMafia.com - Tip of the Day         [dotnetmafia.com]        on Topsy.com

October 20, 2009 3:35 PM
 

Sharepoint 2010 Blog – Luis Valencia » Using SPMonitoredScope said:

Pingback from  Sharepoint 2010 Blog – Luis Valencia » Using SPMonitoredScope

August 19, 2010 10:28 PM

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is an independent SharePoint consultant specializing in ECM, Apps, and Search.
2015 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems