3

I have created a new list definition following the steps listed here: http://msdn.microsoft.com/en-us/library/gg276355.aspx

I can create new lists using this definition in the browser with out any problems. However, I am not able to figure out a way to create lists using code. I tried to follow the instructions listed here:

http://www.learningsharepoint.com/2010/09/05/create-list-from-list-template-sharepoint-2010-programmatically/

But the line SPListTemplate listTemplate = listTemplates["ListTemplateName"]; returns a null.

Also, For some reason I don't see the list definition in the list template gallery. Is there something I need to do, for the new list definition to show up in the gallery?

Any help with this is appreciated.

2 Answers 2

4

First, a list definition and a list template are not the same, even though the api treats them as if they are. As far as I know, list definitions do not appear in the template gallery.

You say you are able to create lists from your custom template using the web interface, so I'll assume you have the feature defined with the list definition in it and that is packaged in a WSP, and that they are installed and activated properly.

The code below is what I used recently when I needed to create several lists in a feature receiver using several custom list templates. Since it is used in a feature receiver it's using the server-side API, the client-side APIs are similar but may not be exact.

This is a compilation of code from several different places I referenced during the hours I spent when first grappling with this. I did rip out some of the extra stuff I was doing, hoping to make it more clear, so if it looks strange in places it is likely because I didn't clean it up well enough.

    private static void SetupLists(SPWeb web)
    {
        using (var newWeb = web.Site.OpenWeb(web.ID))
        {
            SetupCustomLists(newWeb);

            // ... any other lists types that need created ... //

            newWeb.Update();
        }
    }

    private static void SetupCustomLists(SPWeb web)
    {
        var customListTemplate = GetListTemplate(web, "Custom List Template");
        SetupList(web, "CustomList", "A new custom list.", "customlist", customListTemplate);

        // ... any other lists of this type that need created ...
    }

    private static SPListTemplate GetListTemplate(SPWeb web, string templateName)
    {
        var template = web.ListTemplates.OfType<SPListTemplate>().FirstOrDefault(i => i.Name == templateName);
        if (template == null)
            throw new SPException(string.Format("Template {0} not found.", templateName));
        return template;
    }

    private static void SetupList(SPWeb web, string name, string description, SPListTemplate template)
    {
        var webId = web.ID;
        SPList list;
        using (var newWeb = web.Site.OpenWeb(webId))
        {
            list = EnsureList(newWeb, name, description, template);
        }

        // ... perform any additional actions needed on list ...
    }

    private static SPList EnsureList(SPWeb web, string name, string description, SPListTemplate template)
    {
        var list = web.Lists.TryGetList(name);
        if (list != null)
        {
            if (list.BaseTemplate != template.Type)
                throw new SPException(string.Format("List {0} has type '{1}'; but should have type '{2}'.", name, list.BaseTemplate, template.Type));
            return list;
        }

        var id = web.Lists.Add(name, description, template);
        return web.Lists[id];
    }
2
  • Thanks for the answer David. It took me all day yesterday to figure out that list templates and list definitions were actually different :) In order to get the template, I had to use this SPWeb currentWeb = siteCollection.OpenWeb(); foreach (SPListTemplate temp in currentWeb.ListTemplates) { // compare on name and get the template }
    – bava
    Sep 7, 2011 at 15:52
  • Sounds like you're finding the template similar to how I did in the GetListTemplate() method above. Glad you got it working. :)
    – David Culp
    Sep 8, 2011 at 15:08
1

You need to replace ListTemplateName with the name of an actual SharePoint List Template. Here is one of many references that should help:

http://sharepoint-snippets.com/list-template-id-for-sharepoint-lists

Not sure what you're trying to do, but GenericList is probably a good place to start.

You could also use the overide that takes a SPListTemplateType:

http://msdn.microsoft.com/en-us/library/ms413986.aspx

1
  • I am trying to create a new list based on a list template that I created. I tried to get the SPListTemplate instance using the my custom template name and it returns a null. That is no surprise as my custom list template does not show up in the list template gallery. Now if I save a list as a tempalte using "Save as template", everything works fine. That caused me to post this, wondering if there is a different way to get list templates deployed as wsp.
    – bava
    Sep 6, 2011 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.