Question

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

Was it helpful?

Solution

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");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top