2009-07-21 54 views
0

我使用下面的代碼,以獲得所需的SPtemplate:
SPListTemplate template = RootWeb.ListTemplates["TaskTemplate"];如何以編程方式訪問模板?

然而這一說法引發以下錯誤:
Value does not fall within the expected range.

我如何訪問名爲TaskTemplate模板?

回答

3

通過查看您的模板名稱,我會說這是您的自定義模板。要訪問自定義模板,您需要使用下面的代碼。

//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"]; 
+0

感謝它的工作 – Azra 2009-07-21 09:09:45

0

這聽起來像你嘗試使用的列表模板不存在。有幾件事要看:

  1. 你是否在根網站(即RootWeb)內創建你的列表?如果您在子站點中創建列表,則應該從您創建列表的同一站點獲取SPListTemplate

  2. 如果您確定要在相同的站點中創建列表您正在從中檢索列表模板的站點,然後檢查包含您正在使用的列表定義的任何功能是否已被激活。當功能被激活時,列表定義(即SPListTemplate)被添加到當前網站的可用列表模板中。

  3. 我在猜測「TaskTemplate」是指內置的SharePoint任務列表定義 - 如果是自定義定義,請忽略此內容。否則,您應該使用的正確名稱是「任務」。

+0

糟糕!我錯過了「TaskTemplate」是一個自定義模板。請參閱kusek的答案以瞭解如何檢索。 – dariom 2009-07-21 07:34:19