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

如何通过功能接收器和现有列表模板创建Sharepoint列表,sharepoint,moss,Sharepoint,Moss,我在MOSS列表模板库中有一个列表模板,我需要从功能接收器使用此模板创建一个列表。这听起来真的很容易,但我找不到一个方法来做到这一点 SPWeb有一个GetCatalog方法,该方法返回一个SPList,其中包含一个项-我的模板-但它是一个SPListItem,我需要一个SPListTemplate。如何将项目“转换”为正确的类型 谢谢使用对象的方法获取表示自定义模板的对象。然后使用该方法从此模板创建新列表。在代码中,这看起来像这样: using (SPSite site = new SPSit

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

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

谢谢

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

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);
}
foreach (SPListTemplate template in web.ListTemplates)
 {
    if (template.InternalName.Equals("MyTemplateName")
     {
        return template;
     }
 }
使用对象的方法获取表示自定义模板的对象。然后使用该方法从此模板创建新列表。在代码中,这看起来像这样:

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);
}
foreach (SPListTemplate template in web.ListTemplates)
 {
    if (template.InternalName.Equals("MyTemplateName")
     {
        return template;
     }
 }

您必须使用internalname…类似这样的名称:

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);
}
foreach (SPListTemplate template in web.ListTemplates)
 {
    if (template.InternalName.Equals("MyTemplateName")
     {
        return template;
     }
 }

您必须使用internalname…类似这样的名称:

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);
}
foreach (SPListTemplate template in web.ListTemplates)
 {
    if (template.InternalName.Equals("MyTemplateName")
     {
        return template;
     }
 }

所以,我们放弃了,取而代之的是使用一个功能接收器完全从代码中创建列表。ListDefs是一个完整的PITA-C是一种更符合逻辑的创建列表的方法,另外,您还可以通过编写列表升级代码获得额外的好处


谢谢大家。

所以,我们放弃了,取而代之的是使用功能接收器完全从代码创建列表。ListDefs是一个完整的PITA-C是一种更符合逻辑的创建列表的方法,另外,您还可以通过编写列表升级代码获得额外的好处


谢谢大家。

请阅读我的答案。这样,您应该可以从GetCustomListTemplates获得结果,而不仅仅是一个空列表。

阅读我的答案。因此,您应该从GetCustomListTemplates获得一个结果,而不仅仅是一个空列表。

Oh和我尝试使用GetCustomListTemplates,但这返回了一个空集合:Oh和我尝试使用GetCustomListTemplates,但这返回了一个空集合:抱歉,GetCustomListTemplates返回了一个空列表抱歉,GetCustomListTemplates返回空listweb.ListTemplates不包含自定义模板。如果我知道为什么…web.ListTemplates不包含我们的自定义模板,就会被破解。如果我知道原因的话。。。