TryGetList in SharePoint 2013

While using SharePoint  Object model in SharePoint 2007 ,we may use to check the existence of a List using the SharePoint List Collection as below.

  1. SPList desList = web.List["Destin List"];  

If the specified list is found, then the corresponding list object get loaded in desList variable. But if the list does not exist, in that case it will throw Argument Exception. To get rid of this we use to have try-catch section to handle the exception.

In SharePoint 2010 onwards, for handling the null while checking for a list is automatically done by a method. We can use the new Method called TryGetList which is available in SPListCollection class.

  1. using (SPSite site = new SPSite("http://DestinSharePointServer"))  
  2. {
     
    using (SPWeb web = site.OpenWeb()) 
    {  
  3.    SPList desList = web.Lists.TryGetList("Destin List");    
    }    
    }    

If the specified list is not available in the Collection, null will be assigned to the desList.

Ebook Download
View all
Learn
View all