• Ektron 9.0 — What’s New  

    Forums and Resources for Developers and Partners

    Welcome! The Ektron DevCenter includes a wide array of resources to assist our developers and partners. Find what you need here in our developer forum.

    James Stout  

    On Demand Video

    Developer's Introduction to the DxH by eGandalf

    Watch Now, On Demand

Latest from the Forums

Browse Forums

News and Updates

More News
  • Mar 18

    Ektron 9.1 Pilot Program Announced!

    As we get ready to launch v9.1, we are looking for early adopters to work with us in a v9.1 Pilot Program. Complete this survey to find out more.

  • Feb 26

    Old DevCenter Now Archived

    The old DevCenter site (dev.ektron.com) will be available as a read-only archive only. Things to note:

    1. Your username & password is the same on the new site, developer.ektron.com
    2. Going forward, please use the new URL, developer.ektron.com
    3. The old discussion forums and old KB articles are still available as an archive (read-only)

     

  • Jan 17

    Scheduled Maintenance

    There will be scheduled maintenance performed on developer.ektron.com starting at 6:00 PM EST on 01/17/2013. Shouldn't be down for too long, we are upgrading software on the servers and apologize for any inconvenience this may cause.

Featured Code

Visit the Code Library

Simple Search Samples from Ektron Office Hours

Simple Search Samples from Ektron Office Hours

Posted in Code Snippets on Apr. 17, 2014 @ 9:44 AM, by James Stout

During the Ektron Office Hours episode on April 16, 2014, I demoed some simple search code to illustrate how you can easily

  • Retrieve items by tag
  • Use Smart Form fields as part of your result output
  • Retrieve items by multiple tags in a ranked list (closest match on top)

Here's the sample. Apart from removing / changing the Master Page requirement on my template, it should require little/no effort to get up and running on your own instance.

Obviously, it will require that your instance be registered with a search server.

Developed in Ektron 9.0 using SOLR.

<%@ Page Title="Sample Search Page" Language="C#" MasterPageFile="~/Masters/Ektron.Com.master" AutoEventWireup="true" CodeFile="SearchSample.aspx.cs" Inherits="WebForms_SearchSample" %>

<asp:Content ID="Content1" ContentPlaceHolderID="uxHeadHolder" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="uxPageHostHolder" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="uxContentHolder" Runat="Server">
    <div class="container">
        <div class="row">
            <div class="span9 offset3">
                <section>
                    <h1>Content by Tag</h1>
                    <p>
                        Yes, you can retrieve content by tag using the API, but since search normalizes 
                        the data, it can respond much faster. In this example, the page also accepts an
                        alias-able querystring parameter, which you can mask easily with a Regex Alias.
                    </p>
                    <h2>Content Tagged with <asp:Literal ID="uxTagName" runat="server" /></h2>
                    <asp:Repeater ID="uxTagResults" runat="server">
                        <ItemTemplate>
                            <div style="margin-bottom: 1em;">
                                <div>
                                    <a href="<%#Eval("Link") %>" title="<%#Eval("Title") %>"><%#Eval("Title") %></a>
                                </div>
                                <div style="color: rgb(34, 34, 34); line-height: 19px; font-size: 10pt;">
                                    <%#Eval("Summary") %>
                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:Repeater>
                </section>
            </div>
        </div>
    </div>

    <div class="container">
        <div class="row">
            <div class="span9 offset3">
                <section>
                    <h1>More Than Just Summaries</h1>
                    <p>
                        Need to return something from within a Smart Form? The most common practice is to use 
                        Search to return the relevant Content IDs then use the Framework API to retrieve
                        the ContentData and proceed from there to build your output. But if you only need a
                        few fields, you may find it more productive to index those fields not so you can 
                        search for them, but so search can return them.
                    </p>
                    <h2>Whitepaper Downloads</h2>
                    <p><i>With External Links</i></p>
                    <asp:Repeater ID="uxDownloadResults" runat="server">
                        <ItemTemplate>
                            <div style="margin-bottom: 1em;">
                                <div>
                                    <a href="<%#Eval("Link") %>" title="<%#Eval("Title") %>" target="_blank"><%#Eval("Title") %></a>
                                </div>
                                <div style="color: rgb(34, 34, 34); line-height: 19px; font-size: 10pt;">
                                    <%#Eval("Summary") %>
                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:Repeater>
                </section>
            </div>
        </div>
    </div>

    <div class="container">
        <div class="row">
            <div class="span9 offset3">
                <section>
                    <h1>Near-Miss Rewards</h1>
                    <p>
                        Grabbing content with similar keywords or tags is a great way to surface other similar
                        content. Using tags, for example, with OR conditions in between, and sorted by rank
                        so you get the most similarly tagged content on top works really well.
                    </p>
                    <h2>Similar Content</h2>
                    <p><i>Using tags: DefaultContent, WCM, CMS</i></p>
                    <asp:Repeater ID="uxNearMissResults" runat="server">
                        <ItemTemplate>
                            <div style="margin-bottom: 1em;">
                                <div>
                                    <a href="<%#Eval("Link") %>" title="<%#Eval("Title") %>" target="_blank"><%#Eval("Title") %></a>
                                </div>
                                <div style="color: rgb(34, 34, 34); line-height: 19px; font-size: 10pt;">
                                    <%#Eval("Summary") %>
                                </div>
                                <div style="color: green;">
                                    Tagged with: <%#Eval("Tags") %>
                                    <br />
                                    Rank: <%#Eval("Rank") %>
                                </div>
                            </div>
                        </ItemTemplate>
                    </asp:Repeater>
                </section>
            </div>
        </div>
    </div>
</asp:Content>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ektron.Cms.Framework.Search;
using Ektron.Cms.Search;
using Ektron.Cms.Search.Expressions;

public partial class WebForms_SearchSample : System.Web.UI.Page
{
    private Ektron.Cms.Framework.Search.SearchManager Searcher = new Ektron.Cms.Framework.Search.SearchManager();
    private AdvancedSearchCriteria advCriteria = null;
    private KeywordSearchCriteria kwdCriteria = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        LoadContentByTag();
        LoadContentWithFieldLink();
        LoadNearMissContent();
    }

    protected override void Render(HtmlTextWriter writer)
    {
        Page.Title = "Search Samples";
        base.Render(writer);
    }

    private void LoadContentByTag()
    {
        string default_tag = "DefaultContent";
        string tag = default_tag;
        if (!string.IsNullOrEmpty(Request.QueryString["tag"]))
        {
            tag = Request.QueryString["tag"];
        }
        uxTagName.Text = tag;

        advCriteria = new AdvancedSearchCriteria();
        advCriteria.ExpressionTree = SearchContentProperty.Id > 0;
        advCriteria.ExpressionTree &= SearchContentProperty.Tags.Contains(tag);
        advCriteria.ExpressionTree &= (SearchContentProperty.FolderIdPath.Contains("73") | SearchContentProperty.FolderIdPath.Contains("77"));

        advCriteria.PagingInfo = new Ektron.Cms.PagingInfo(3);

        advCriteria.OrderBy = new List<OrderData>()
        {
            new OrderData(SearchContentProperty.DateCreated, OrderDirection.Descending)
        };

        advCriteria.ReturnProperties.Add(SearchContentProperty.Id);
        advCriteria.ReturnProperties.Add(SearchContentProperty.DateCreated);
        advCriteria.ReturnProperties.Add(SearchContentProperty.Description); // manual summary
        advCriteria.ReturnProperties.Add(SearchContentProperty.QuickLink);
        advCriteria.ReturnProperties.Add(SearchContentProperty.Title);

        var searchResponse = Searcher.Search(advCriteria);

        var outputItems = searchResponse.Results.Select(s => new {
            Title = s[SearchContentProperty.Title],
            Link = s[SearchContentProperty.QuickLink],
            Summary = s[SearchContentProperty.Description]
        });

        uxTagResults.DataSource = outputItems;
        uxTagResults.DataBind();
    }

    private void LoadContentWithFieldLink()
    {
        advCriteria = new AdvancedSearchCriteria();
        advCriteria.ExpressionTree = SearchContentProperty.Id > 0;
        advCriteria.ExpressionTree &= SearchContentProperty.XmlConfigId.EqualTo(24);
        advCriteria.ExpressionTree &= SearchContentProperty.FolderId.EqualTo(173);

        advCriteria.PagingInfo = new Ektron.Cms.PagingInfo(3);

        advCriteria.OrderBy = new List<OrderData>()
        {
            new OrderData(SearchContentProperty.DateCreated, OrderDirection.Descending)
        };

        advCriteria.ReturnProperties.Add(SearchContentProperty.Id);
        advCriteria.ReturnProperties.Add(SearchContentProperty.DateCreated);
        advCriteria.ReturnProperties.Add(SearchContentProperty.Description); // manual summary
        advCriteria.ReturnProperties.Add(SearchSmartFormProperty.GetStringProperty("/FieldCollection/LinkURL"));
        advCriteria.ReturnProperties.Add(SearchContentProperty.Title);
        advCriteria.ReturnProperties.Add(SearchContentProperty.XmlConfigId);

        var searchResponse = Searcher.Search(advCriteria);

        var outputItems = searchResponse.Results.Select(s => new
        {
            Title = s[SearchContentProperty.Title],
            Link = s[SearchSmartFormProperty.GetStringProperty("/FieldCollection/LinkURL")],
            Summary = s[SearchContentProperty.Description]
        });

        uxDownloadResults.DataSource = outputItems;
        uxDownloadResults.DataBind();
    }

    private void LoadNearMissContent()
    {
        kwdCriteria = new KeywordSearchCriteria();
        kwdCriteria.QueryText = "ebttags:DefaultContent OR ebttags:WCM OR ebttags:CMS";
        kwdCriteria.ExpressionTree = SearchContentProperty.Id > 0;
        kwdCriteria.ExpressionTree &= (SearchContentProperty.FolderIdPath.Contains("73") | SearchContentProperty.FolderIdPath.Contains("77"));

        kwdCriteria.PagingInfo = new Ektron.Cms.PagingInfo(5);

        kwdCriteria.ReturnProperties.Add(SearchContentProperty.Id);
        kwdCriteria.ReturnProperties.Add(SearchContentProperty.Description); // manual summary
        kwdCriteria.ReturnProperties.Add(SearchContentProperty.QuickLink);
        kwdCriteria.ReturnProperties.Add(SearchContentProperty.Title);
        kwdCriteria.ReturnProperties.Add(SearchContentProperty.Rank);
        kwdCriteria.ReturnProperties.Add(SearchContentProperty.Tags);

        var searchResponse = Searcher.Search(kwdCriteria);

        var outputItems = searchResponse.Results.Select(s => new
        {
            Title = s[SearchContentProperty.Title],
            Link = s[SearchContentProperty.QuickLink],
            Summary = s[SearchContentProperty.Description],
            Tags = s[SearchContentProperty.Tags],
            Rank = s[SearchContentProperty.Rank]
        });

        uxNearMissResults.DataSource = outputItems;
        uxNearMissResults.DataBind();
    }
}
| 0 comments | Tested in N/A

Our Knowledge Base

Need Help?

Here are some of our newest and our most commonly requested Knowledge Base articles.

Ektron Expert’s Corner

See More Posts

What Do You Think of our New Design?

    

Have more to say? We’d love to hear it!