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];
}