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;

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

Cool eh?



comments powered by Disqus


Recent Tweets

Maybe resolved itself, after resetting emulator and couple restarts - will let you know
@MillerMark A few weeks ago you tweeted that something huge/incredible was coming....what is/was it? (I am hoping s……
Oddly, it won't hit breakpoints in XF .net standard 2 project now either
@JamesMontemagno Updated to 15.7 preview 2 Visual Studio - now having lots of odd thread issues when starting debug……