TryGetList – SharePoint 2010 Object Model Enhancements

Any developer that worked with WSSv3 found out that there was no “nice and easy” way to check if a SPList existed and handle it accordingly. One of the easiest and most popular methods (no pun intended!) was to handle it with a try catch block. This worked effectively and it was part of every utilities class that was used when working with SharePoint.

SPSite currentSite = SPContext.Current.Site;
SPList mySPList = currentSite.RootWeb.Lists.TryGetList("ListName");


if (mySPList != null)
{
  ...
}

I was not even aware that SharePoint 2010 added a method to handle this automatically until I stumbled upon a TryGetList method within SPWeb.Lists. In SharePoint 2010 you no longer need an extension of any sorts to help handle getting lists, you can just use the TryGetList method and everything else will be taken care of! If the list does not exist then the object will just be null with no exception being thrown. You can check out the full documentation on MSDN here.

Advertisements

About Joseph Velliah
As a SharePoint Developer my professional interests tend to be technical and SharePoint focused. I run a blog at "SP RIDER" where you can expect to read HOW TOs and scenarios that I run into during my day to day job. I hope my posts will give back a little to the community that is helped me.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: