SPListCollection.TryGetList Method
Returns the list with the specified title from the collection, but returns null instead of an exception if the list does not exist.
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
Parameters
- listTitle
- Type: System.String
A string that contains the title of the list.
{
try
{
using (SPWeb oWebsiteRoot = SPContext.Current.Web)
{
Take care if you really use this code (example?).
- oSiteCollection isn't used
- Disposing shouldn't be done on SPContent.Current....
- 4/27/2012
- Stefan_Lankester
Ex :
Public Shared Function TryGetListByUrl(ByVal web As SPWeb, ByVal urlList As String) As SPList
Try
Return web.GetList(urlList)
Catch ex As Exception
Return Nothing
End Try
End Function
- 1/20/2012
- Samuel Levesque
protected void WorkWithList()
{
using (SPSite oSiteCollection = SPContext.Current.Site)
{
try
{
using (SPWeb oWebsiteRoot = SPContext.Current.Web)
{
TargetURL = HttpUtility.UrlPathEncode(TargetURL);
//usual method
try
{
SPList oList = oWebsiteRoot.GetListFromUrl(SPContext.Current.Web.Url + "/Lists" + TargetURL + "/AllItems.aspx");
//carry out list operations
}
catch (SPException spe)
{
//cannot find list or columns not correct
}
//alternative if referencing list from list
//title no exception handling required in theory
//as null returned if list not found
SPListCollection oListCollection = oWebsiteRoot.Lists;
SPList oList2 = oListCollection.TryGetList("youlist");
}
}
catch (Exception ex)
{
//problem getting to list
}
}
}
- 8/2/2011
- tristian2
"http://ranaictiu-technicalblog.blogspot.com/2010/04/sharepoint-2010-new-method-added-to.html
- 4/5/2010
- Sohel Rana