ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

Find Sharepoint List Anyway

this method find your list without any Exception by List Name , Title and ListID

Source

public static SPList FindListAnyway(this SPWeb web, string listName, Guid ListId)
{
    if (web == null)
    {
        return null;
    }
    SPList lst = null;

    try
    {
        lst = web.Lists.TryGetList(listName);
        if (lst == null)
        {
            lst = web.Lists[ListId];
        }

    }
    catch
    {

        if (lst == null)
        {
            foreach (SPList l in web.Lists)
            {
                if (string.Compare(l.Title, listName, true) == 0)
                {
                    lst = l;
                    break;
                }
            }
        }

    }



    return lst;

}

Example

Guid _ListID = new Guid("AA953E63-FA0C-4546-B1AF-0AD816596A38");
SPList list = mySpWeb.FindListAnyway("PhoneBook", _ListID );

Author: http://mb-seifollahi.ir

Submitted on: 25 mrt 2013

Language: C#

Type: Microsoft.SharePoint.SPList

Views: 4737