Search code examples
sitecoresitecore7

sitecore identify template type in treelist view


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>";
    }

}

Solution

  • 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