如何通过功能接收器和现有列表模板创建Sharepoint列表

时间:2009-05-28 19:32:04

标签: sharepoint moss

我在MOSS列表模板库中有一个列表模板,我需要使用此模板从功能接收器创建一个列表。这听起来很容易,但我找不到这样做的方法。

SPWeb有一个GetCatalog方法,它返回一个带有1个项目的SPList - 我的模板 - 但它是一个SPListItem,我需要一个SPListTemplate。如何将项目“转换”为正确的类型?

由于

4 个答案:

答案 0 :(得分:0)

使用GetCustomListTemplates对象的SPSite方法获取代表自定义模板的SPListTemplate对象。然后使用SPListCollection.Add方法从此模板创建新列表。在代码中,这看起来像这样:

using (SPSite site = new SPSite("http://server/sites/site"))
using (SPWeb web = site.OpenWeb())
{
  SPListTemplateCollection templates = site.GetCustomListTemplates(web);
  SPListTemplate template = templates["MyTemplates"];
  Guid listId = web.Lists.Add("Title", "Description", template);
}

答案 1 :(得分:0)

您必须使用内部名称......如下所示:

foreach (SPListTemplate template in web.ListTemplates)
 {
    if (template.InternalName.Equals("MyTemplateName")
     {
        return template;
     }
 }

答案 2 :(得分:0)

所以,我们放弃了,而是使用了一个功能接收器来完全从代码创建列表。 ListDefs是一个完整的PITA - C#是一种更加合理的创建列表的方式,而且您还可以获得能够将升级代码升级到列表的额外好处。

谢谢大家。

答案 3 :(得分:0)

阅读我对此question的回答。有了它,你应该从GetCustomListTemplates而不是只是一个空列表得到一个结果。