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

@jeffrey_doucet @Brian_Bancroft Canadians, self define themselves as NOT American, rather than being Proud Canadians
Retweeted by @dyardy Why do so many Canadians obsess over American policy that won’t impact us while being fairly ambivalent to day-to-d… twitter.com/i/web/status/9…
@melrobbins I was looking for 50$ earlier today, and still can't find it...ideas :)
Trump Watches Up To 8 Hours Of TV Per Day | HuffPost #SmartNews ===that explains everything huffingtonpost.com/entry/trump-te…
Retweeted by @dyardy Boston https://t.co/QmtMIF8P0F