SharePoint: SPList.HasView() extension method

One quick posting. I wrote yesterday extension method for SharePoint that checks if view exists. I think it is far better for performance if I don’t live on try…catch to find out if view exists or not. Extension method that you have to put in static class is here.

[CLSCompliant(false)]
public static bool HasView(this SPList list, string
viewName)
{
   
if (string
.IsNullOrEmpty(viewName))
       
return false
;

   
foreach (SPView view in
list.Views)
       
if
(view.Title.ToLowerInvariant() == viewName.ToLowerInvariant())
           
return true
;

   
return false;
}
Liked this post? Empower your friends by sharing it!

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    One thought on “SharePoint: SPList.HasView() extension method

    • June 11, 2010 at 2:32 pm
      Permalink

      Good.
      But i think the performance would be bad when you have 1000+ views. try catch ftw!

    Leave a Reply

    Your email address will not be published. Required fields are marked *