Check if list exist using SharePoint 2010 object model

November 26, 2011

hi guys,   Here's a quick tip for checking if a list exist.   Every one who used Moss 2007 knows that when you want to check if the list exist,   You always need to use try catch for exceptions.   Well friends in Sharepoint 2010, Microsoft did it for us and made a simple   Property called TryGetList that returns null if the list isnt exist.   Example using (SPSite site =newSPSite( "http://MyStie")) {     using (SPWeb web = site.OpenWeb()) {            SPList list = web.Lists.TryGetList("MyList");        if (list != null) {               //You got your list!! }   }   }   That's all folks :)
no comments