Monday, March 18, 2013

SharePoint - Check list exists or not in a site using Trygetlist() method

In SharePoint, most of the data operations can be done through List\ Library. We can access the list in SPObjectModel to get the data. If site doesn't contain any list with given name, it will return null reference exception. To fix that we have a methods in SPObjectModel, TryGetList(String List Name).Here is the code to analyse the both methods

in the above code, if there is no list with name "MyList", it will return null reference exception in line SPList currentList = currentWeb.Lists["MyList"]; to avoid this we will write the code placing the line in try, catch block. But it is not a good to code to place try catch blocks regularly. In SPObjectModel we have a built in method "TryGetList(string List Name). If there is list with given name, it will return SPList object. If there is no list with given name it will return null.

Share this

Follow us in facebook

About Me

Having good experience in SharePoint 2007/2010 and .net applications. Currently, working on SharePoint 2013 and training people on SharePoint. Microsoft certified technology specialist, Microsoft Certified Professional developer in SharePoint 2010.