Thursday, June 23, 2011

Simple ways to check if List exist in MOSS using Lambda expressions

In MOSS we don't have a pre defined method of to check if a List Exist .Here comes new feature of  C# to extensions to rescue.

public static bool ListExists(this SPWeb web, string listName)
        {
            return web.Lists.Cast().Any(l => l.Title.Equals(listName, StringComparison.OrdinalIgnoreCase));
        }
Alternate for the Same

public static bool ListExists(SPWeb web, string listName)
         {
            var lists = web.Lists;
            foreach (SPList list in lists)
            {
                if(list.Title.Equals(listName))
                    return true;
            }
            return false;
       }

No comments:

Post a Comment