Archives

Archives / 2013 / September
  • 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.