SPListCollection ContainsList Extension Method

This is such a simple thing but something every SharePoint developer should have in their toolkit (well, actually, this is something Microsoft should put into the product). The SPFieldCollection has a nice method called ContainsField so you can check for the existence of a field (without throwing an exception if you get the spelling wrong). This is my version of the same method for SharePoint Lists.

 public static class SPListExtensions
{
public static bool ContainsList(this SPListCollection lists, string displayName)
{
try
{
return lists.Cast<SPList>().Any(list => string.Equals(list.Title, displayName, StringComparison.OrdinalIgnoreCase);
}
catch (Exception)
{
return false;
}
}
}
Enjoy.

1 Comment

  • It would probably be better to use:

    string.Equals(l.Title, displayName, StringComparison.OrdinalIgnoreCase)

    Calling .ToLower creates a new copy of the string; passing a StringComparison doesn't.

Comments have been disabled for this content.