Reputation: 507
I have a treelist which allows an editor to define a list of links which is then output in a sublayout.
Depending upon which templates the selected pages use in that treelist field will determine the field values I need to write out.
I have a generic content page template where its own sitecore URL should be referenced but I then have a pseudo template which contains a field for a string URL and additional field params relating to to an external site.
In that scenario I don't want the sitecore URL I instead need its field values so as to concat a link to an external site along with the token details and present that to the user in the list of links.
Currently I have the code below but I need to include a condition that says if the template of the current GUID item is type 'SSO-Link' then don't retrieve its sitecore URL from linkmanager instead refer to a field called URL as well as a number of additional fields.
Thanks - current code below
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>";
}
}
Upvotes: 0
Views: 166
Reputation: 1445
I use an extension for my templates and template items. Then I call a constants class for the ID of the template I am comparing to.
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));
}
}
Constants
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}"));
}
Use
if (item.IsDerived(Products.ProductDetail))
{ if code here }
Hope this helps.
Upvotes: 0
Reputation: 27152
From what I understand, you need to add this simple condition at the beginning of your loop:
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
Upvotes: 2