tags:

views:

161

answers:

2

hi I am usgign the followign code to get the required SPtemplate.. SPListTemplate temp = RootWeb.ListTemplates["TaskTemplate"]; however this statement throwing an error as value does not fall within the expected range.

please help there is a template present named TaskTemplate

+3  A: 

By looking at the Name of your template I would say that is your Custom Template. For accssing the custom template you need to use the below code.

//This gives you only the built-in template or templates deployed using Features
foreach (SPListTemplate item in oWeb.ListTemplates)
{
    Console.WriteLine(item.Name);
}

//This gives you the custom template created by you
foreach (SPListTemplate item in oSite.GetCustomListTemplates(oWeb))
{
    Console.WriteLine(item.Name);                        
}
//So for your requirement you need to use
oSite.GetCustomListTemplates(oWeb)["TaskTemplate"]
Kusek
thanks it worked
Azra
A: 

It sounds like the list template your trying to use does not exist. There are a few of things to look at:

  1. Are you creating your list within the root site (i.e. RootWeb)? If you're creating a list in a sub-site you should get the SPListTemplate from the same site that you're creating the list in.

  2. If you're sure that you're creating the list within the same site that you're retrieving the list template from, then check that any features that contain the list definition you're working with have been activated. When the feature is activated, the list definition (i.e. SPListTemplate) gets added to the current site's available list templates.

  3. I'm guessing that "TaskTemplate" refers to the built-in SharePoint task list definition - if it is a custom definition, ignore this. Otherwise, the correct name you should be using is "Tasks".

dariom
Oops! I missed that "TaskTemplate" was a custom template. See kusek's answer for how to retrieve that.
dariom