SharePoint - 确定创建网站的自定义网站模板

SharePoint - Determine the custom site template a site was created from
2021-11-25
  •  译文(汉语)
  •  原文(英语)

我使用"站点设置"来"将站点另存为模板",然后使用该自定义站点模板创建几个站点(或者我应该将其称为解决方案?)

如何让我的 C# 计算出从这个自定义站点模板创建的站点?

请注意,无论站点是否使用此自定义模板创建,SPWeb 的 .WebTemplate、.WebTemplateId 和 .Configuration 都会分别返回"STS"、1 和 1.我认为这是因为他们正在报告基础 SharePoint 开箱即用模板.

提前致谢.

解决过程1

为什么不在网络的 PropertyBag 中添加一个条目?以"web_template_id"为键,以模板的ID或名称为值.您可以使用 SharePoint Designer 轻松完成此操作,并且 PropertyBag 作为 Web 模板 WSP 的一部分包含在内.

或者,如果将模板导入 Visual Studio,则可以声明性地将条目添加到 Web 的属性包中,如下所示:

<Property Name="web_template_id" Value="SiteTemplates.TemplateName" Type="string" />

这是从代码中 SPWeb 对象的 AllProperties 集合中获取自定义模板名称的简单案例.

解决过程2

来自http://www.go4sharepoint.com/Forum/determine-site-template-site-11323.aspx

您可以使用 SPWeb 对象的 WebTemplate 和 Configuration 属性的组合来找出最初构建站点的模板.WebTemplate 将包含类似"STS"的内容,它是包含站点定义的 12 hive Template/sitetemplates 目录中子目录的名称.配置将对应于该子目录中 onet.xml 内的配置 ID.但是,这有几个问题.

这是与 SharePoint 2007 相关的参考,但我相信核心概念仍然适用,除了对"12"配置单元的任何引用外,都应参考"14"配置单元.

解决过程3

要了解发生了什么,您需要了解 SharePoint 中站点定义和 Web 模板之间的区别:

尽管经常互换使用,但它们是不同的,并且在 SharePoint 中的处理方式不同.站点定义,无论是现成的还是自定义的,都位于 14 hive\TEMPLATE\SiteTemplates 中.另一方面,Web 模板将站点定义实例的自定义保存到 .stp 或 .wsp 文件,这允许在其他站点实例中复制该站点.

所以,真正的问题是:即使是 Microsoft 也对站点定义和 Web 模板之间的区别感到困惑.因为当他们命名 WebTemplate 属性时,他们指的是站点定义,而不是 Web 模板.

请注意 MSDN 所说的:

SPWeb.WebTemplate 属性

当您通过将网站另存为模板来创建自定义网站模板,然后从该模板创建新网站时,WebTemplate 属性包含自定义模板派生自网站定义的名称,而不是自定义模板的名称.因此,如果用于创建自定义模板的网站本身是根据标准团队网站定义创建的,则从新模板创建的所有网站的 WebTemplate 属性将返回"STS"

SPWeb.Configuration 属性

通过将站点另存为模板来创建站点模板,然后从该模板创建站点时,Configuration 属性 包含站点模板派生自的站点定义配置的 ID.

(强调我的)

这支持您在问题中发现的内容.那么您如何确定用于创建站点的自定义 Web 模板呢?我不确定这是可能的.因为如果可能的话,我希望 MSDN 能再提供一两句话,告诉他们如何完成他们刚刚解释的这些属性没有做的事情.此外,在四处寻找时,我发现这个问题被问了几次,但从来没有一个令人满意的答案.

I use "Site Settings" to "Save Site as Template", and then create a couple of sites using that custom site template (or should I call it a Solution?)

How can I get my C# to work out which sites were created from this custom site template?

Note that SPWeb's .WebTemplate, .WebTemplateId and .Configuration all come back "STS", 1 and 1 respectively regardless of whether the site was created using this custom template or not. I assume that that is because they are reporting on the underlying SharePoint out of the box template.

Thanks in advance.

Solutions1

Why not add an entry into the web's PropertyBag? With a key of "web_template_id" and the ID or name of the template as the value. You can do this easily with SharePoint Designer and the PropertyBag is included as part of the web template WSP.

Alternatively, if you import the template into Visual Studio you can add an entry into the web's property bag declaratively, like so:

<Property Name="web_template_id" Value="SiteTemplates.TemplateName" Type="string" />

It's then a simple case of fetching the custom template name from the AllProperties collection of the SPWeb object in your code.

Solutions2

From http://www.go4sharepoint.com/Forum/determine-site-template-site-11323.aspx

You can use a combination of the WebTemplate and Configuration properties of the SPWeb object to find out which template the site was originally built from. The WebTemplate will contain something like "STS" which is the name of the subdirectory in the 12 hive Template/sitetemplates directory containing the site definition. The Configuration will correspond to the Configuration ID inside the onet.xml in that subdirectory. However there are a couple issues with this.

This is a reference that is pertains to SharePoint 2007, but I believe the core concepts still apply, except for any reference to the "12" hive, should refer to the "14" hive.

Solutions3

To understand what is happening, you need to understand the difference in SharePoint between a Site Definition and a Web Template:

Although often used interchangeably, they are different and are treated differently within SharePoint. Site Definitions, whether out of the box or custom, live in 14 hive\TEMPLATE\SiteTemplates. On the other hand, Web Templates save customizations to an instance of a Site Definition to a .stp or .wsp file, which allows that site to be replicated in other site instances.

So, here is the real problem: even Microsoft gets confused in the difference between Site Definitions and Web Templates. Because when they named the WebTemplate property, they meant Site Definition, not Web Template.

Notice what MSDN says:

SPWeb.WebTemplate Property

When you create a custom site template by saving a site as a template and then create a new site from that template, the WebTemplate property contains the name of the site definition from which the custom template derives, not the name of the custom template. Thus if the site that was used to create a custom template was itself created from the standard team site definition, the WebTemplate property of all sites that are created from the new template will return "STS"

SPWeb.Configuration Property

When you create a site template by saving a site as a template and then create a site from that template, the Configuration property contains the ID of the site definition configuration from which the site template derives.

(emphasis mine)

This supports what you discovered in your question. So how do you determine what custom Web Template was used to create a site? I'm not sure it is possible. Because if it was possible, I would hope that MSDN would have provided another sentence or two telling how to accomplish what they just explained these properties don't do. Also, in searching around, I have found this question asked a couple times, but there has never been a satisfactory answer.

转载于:https://stackoverflow.com/questions/15238040/sharepoint-determine-the-custom-site-template-a-site-was-created-from

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入