SharePoint Method–Return SPList (or Create a new one)

Method that returns an existing SPList on a SharePoint web (based on list name) or create a new one if not found.

public static SPList EnsureList(SPWeb site, string listName, SPListTemplateType template, bool onQuickLaunch) {
    
    SPList list = null;
    Guid listID = Guid.Empty;
    if (site != null) {
        foreach (SPList item in site.Lists) {
            if (item.Title.ToLower() == listName.ToLower()) {
                list = item;
                listID = item.ID;
                break;
            }
        }

        if (list == null) {
            listID = site.Lists.Add(listName,"", template);
            list = site.Lists[listID];
            list.OnQuickLaunch = onQuickLaunch;
            list.Update();            
        }
    } else {
        throw new Exception("In EnsureSiteDataList SPWeb is null");
    }
    return list;
}

Cool eh?

Author

David

comments powered by Disqus

Categories

Recent Tweets

Retweeted by @dyardy Hey C# peeps, we’re trying C# in browser with reference content. Try it here: docs.microsoft.com/dotnet/csharp/… reply with feedback /cc @LadyNaggaga
23 Amazing Vintage Photographs Taken Inside WWII Tank Factories ~ vintage everyday vintag.es/2017/10/23-ama…
When is AI NOT Search? Artificiality intelligence==Search Engine (there cases when this is not true)
@projectedxyz Large data vs big data? Industry has so misused buzz words to sell products, and so you end up nothi… twitter.com/i/web/status/9…