sitecore 在树列表视图中识别模板类型
sitecore identify template type in treelist view
我有一个树列表,允许编辑器定义 link 的列表,然后在子布局中输出。
根据所选页面在该树列表字段中使用的模板,将确定我需要写出的字段值。
我有一个通用的内容页面模板,其中应该引用它自己的 sitecore URL,但我有一个伪模板,其中包含一个字符串字段 URL 和与一个外部网站。
在那种情况下,我不想要 sitecore URL,而是需要它的字段值,以便将 link 与令牌详细信息一起连接到外部站点,并将其呈现给link 列表中的用户。
目前我有下面的代码,但我需要包含一个条件,说明如果当前 GUID 项的模板类型为 'SSO-Link',则不要从 [= 检索其 sitecore URL 24=]manager 而是引用一个名为 URL 的字段以及许多其他字段。
谢谢 - 当前代码如下
Item[] selectedItems = treelistField.GetItems();
foreach (Item item in selectedItems)
{
string itemName = item.Name;
string displayName = item.DisplayName;
string url = LinkManager.GetItemUrl(item);
string linkName = "Undefined";
if (displayName != null)
{
linkName = displayName;
}
else if (itemName != null)
{
linkName = itemName;
}
if (linkName != "Undefined" && url != null)
{
htmlOutput.Text += "<a href=\"" + url + "\">";
htmlOutput.Text += linkName;
htmlOutput.Text += "</a>";
}
}
据我了解,您需要在循环开头添加这个简单的条件:
foreach (Item item in selectedItems)
{
string url = null;
if (item.TemplateName == "SSO-Link")
{
url = item["URL"];
// other fields
}
else
{
url = LinkManager.GetItemUrl(item);
}
// your code
我为我的模板和模板项目使用了一个扩展。然后我为我正在比较的模板的 ID 调用常量 class。
public static class TemplateExtensions
{
public static bool IsDerived([NotNull] this Template template, [NotNull] ID templateId)
{
return template.ID == templateId || template.GetBaseTemplates().Any(baseTemplate => IsDerived(baseTemplate, templateId));
}
public static bool IsDerived([NotNull] this TemplateItem template, [NotNull] ID templateId)
{
return template.ID == templateId || template.BaseTemplates.Any(baseTemplate => IsDerived(baseTemplate, templateId));
}
}
常量
public static class Products
{
public static TemplateID ProductSection = new TemplateID(new ID("{73400360-5935-40B6-88BC-350DC5B9BC90}"));
public static TemplateID ProductDetail = new TemplateID(new ID("{9CD3D5ED-E579-4611-88E0-6B44C9D56F16}"));
}
使用
if (item.IsDerived(Products.ProductDetail))
{ if code here }
希望对您有所帮助。
我有一个树列表,允许编辑器定义 link 的列表,然后在子布局中输出。
根据所选页面在该树列表字段中使用的模板,将确定我需要写出的字段值。
我有一个通用的内容页面模板,其中应该引用它自己的 sitecore URL,但我有一个伪模板,其中包含一个字符串字段 URL 和与一个外部网站。
在那种情况下,我不想要 sitecore URL,而是需要它的字段值,以便将 link 与令牌详细信息一起连接到外部站点,并将其呈现给link 列表中的用户。
目前我有下面的代码,但我需要包含一个条件,说明如果当前 GUID 项的模板类型为 'SSO-Link',则不要从 [= 检索其 sitecore URL 24=]manager 而是引用一个名为 URL 的字段以及许多其他字段。
谢谢 - 当前代码如下
Item[] selectedItems = treelistField.GetItems();
foreach (Item item in selectedItems)
{
string itemName = item.Name;
string displayName = item.DisplayName;
string url = LinkManager.GetItemUrl(item);
string linkName = "Undefined";
if (displayName != null)
{
linkName = displayName;
}
else if (itemName != null)
{
linkName = itemName;
}
if (linkName != "Undefined" && url != null)
{
htmlOutput.Text += "<a href=\"" + url + "\">";
htmlOutput.Text += linkName;
htmlOutput.Text += "</a>";
}
}
据我了解,您需要在循环开头添加这个简单的条件:
foreach (Item item in selectedItems)
{
string url = null;
if (item.TemplateName == "SSO-Link")
{
url = item["URL"];
// other fields
}
else
{
url = LinkManager.GetItemUrl(item);
}
// your code
我为我的模板和模板项目使用了一个扩展。然后我为我正在比较的模板的 ID 调用常量 class。
public static class TemplateExtensions
{
public static bool IsDerived([NotNull] this Template template, [NotNull] ID templateId)
{
return template.ID == templateId || template.GetBaseTemplates().Any(baseTemplate => IsDerived(baseTemplate, templateId));
}
public static bool IsDerived([NotNull] this TemplateItem template, [NotNull] ID templateId)
{
return template.ID == templateId || template.BaseTemplates.Any(baseTemplate => IsDerived(baseTemplate, templateId));
}
}
常量
public static class Products
{
public static TemplateID ProductSection = new TemplateID(new ID("{73400360-5935-40B6-88BC-350DC5B9BC90}"));
public static TemplateID ProductDetail = new TemplateID(new ID("{9CD3D5ED-E579-4611-88E0-6B44C9D56F16}"));
}
使用
if (item.IsDerived(Products.ProductDetail))
{ if code here }
希望对您有所帮助。