Checking if a list exists before adding a new one using feature?

1.9k Views Asked by At

I am trying to create a few lists using features so that these custom lists are created automatically as soon as the feature is activated on a team site. The feature is scoped at a site level. Is it possible to check weather a list already exists in a site collection before adding a new list using feature? I tried to do this check on feature activated event which gave me error saying list with same name already exists. Any help will be greatly appreciated. Thanks

1

There are 1 best solutions below

6
On BEST ANSWER
public static class SPWebHelper
{
    public static bool IsListExistByTitle(this SPWeb web, string title)
    {
         return web.Lists.Cast<SPList>().FirstOrDefault(
                        list => list.Title == title) != null;
    }


    public static bool IsListExistByInternalName(this SPWeb web, string internalName)
    {
         return web.Lists.Cast<SPList>().FirstOrDefault(
                        list => list.RootFolder.Name == internalName)!=null;
    }
}

call extension function

bool existlist = SPContext.Current.Web.IsListExistByTitle("YourListTitle");