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
Asked
Active
Viewed 1,920 times
1 Answers
1
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");

vvk
- 833
- 5
- 16
-
in this Lists.Cast
gives an error do we need to add any reference or any using directive? – Sree Kuttan Jul 25 '12 at 08:54 -
add your cs file: using Microsoft.SharePoint; – vvk Jul 25 '12 at 09:32
-
its already added still error exist. i'm using sp 2010 nd vs 2010 c# – Sree Kuttan Jul 25 '12 at 09:40
-
should also be: using System.Linq; – vvk Jul 25 '12 at 09:44
-
nope it do not work. it gives error for saying "Extention method must be defined in a non-generic static class". it would be as its a static method tried using it as a non static method that do not work either. – Sree Kuttan Jul 25 '12 at 10:05
-
Thank you very much for your help. Just a slight altercation to be done as in features SPContext is not used rather we use properties object hence i had to eliminate SPContext instead used the web object came from properties. Thanks once again. – Sree Kuttan Jul 25 '12 at 10:35