andreas.ek@internetfabriken.se
Mar 22, 2009
  8125
(0 votes)

Easy made search function for EPiServer

Technorati-taggar: ,

This is an easy example to find pages from a seachword into a pagedatacollection in EPiServer PageTextIndex database.

Header for “using”:

---

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiServer;
using EPiServer.Core;

---

And the code to do the search in the EPiServer PageTextIndex database:

---

/// <summary>
/// Simple use of PageSearch with default value for PageID and MaxCount.
/// </summary>
/// <param name="SearchWord">The word to search for</param>
/// <returns>PageDataCollection</returns>
public static PageDataCollection PageSearch(string SearchWord)
{
    return PageSearch(SearchWord, PageReference.StartPage.ID, 100);
}

/// <summary>
/// Make a quick search to the EPiServer PageTextIndex database with this function.
/// </summary>
/// <param name="SearchWords">The word to search for</param>
/// <param name="PageID">Page ID of the root page to search from</param>
/// <param name="MaxCount">Maximum count of pages in the result</param>
/// <returns>PageDataCollection</returns>

public static PageDataCollection PageSearch(string SearchWord, int PageID, int MaxCount)
{
    PageDataCollection pages = new PageDataCollection();
    bool onlywholeword = false;

    EPiServer.Core.Html.TextSearchParameters searchparam = new EPiServer.Core.Html.TextSearchParameters(SearchWord, onlywholeword);

    if (searchparam.ContainsSearchWords)
    {
        EPiServer.Core.Html.TextSearchResults result = null;
        EPiServer.DataAccess.PageTextIndexDB xdb = new EPiServer.DataAccess.PageTextIndexDB();
        result = xdb.SearchPages(searchparam, PageID, MaxCount);

        List<EPiServer.Core.Html.PageRank> list = result.ToList();
        foreach (EPiServer.Core.Html.PageRank pageRank in list)
        {
            EPiServer.Core.Html.TextSearchMatch match = pageRank as EPiServer.Core.Html.TextSearchMatch;
            if (match != null)
            {
                pages.Add(match.PageData);
            }
        }
    }
    return pages;
}

 

Any other suggestion to easy search for content in EPiServer pages? It would be great!

Mar 22, 2009

Comments

Sep 21, 2010 10:32 AM

Well, you could use the SearchDataSource

eg:
pagedDataSource.DataSource = searchDataSource.Select(new DataSourceSelectArguments());
/ Flalar

Sep 21, 2010 10:32 AM

You're using the EPiServer.DataAccess namespace directly, which is kind of not allowed.

/Steve

Sep 21, 2010 10:32 AM

The EPiServer police may arrest you:)
/ Anders Hattestad

Sep 21, 2010 10:32 AM

Yes, it's a risk I will take to get happy customers! ;-)

Please login to comment.
Latest blogs
Opticon 2024 - highlights

I went to Opticon in Stockholm and here are my brief highlights based on the demos, presentations and roadmaps  Optimizely CMS SaaS will start to...

Daniel Ovaska | Sep 27, 2024

Required fields support in Optimizely Graph

It's been possible to have "required" properties (value must be entered) in the CMS for a long time. The required metadata haven't been reflected i...

Jonas Bergqvist | Sep 25, 2024

How to write a bespoke notification management system

Websites can be the perfect vehicle for notifying customers of important information quickly, whether it’s the latest offer, an operational message...

Nicole Drath | Sep 25, 2024

Optimizely DAM – An Introduction

I presented a talk about the Optimizely DAM at the OMVP summit during Opticon 2024 in Sweden. I have now converted that talk into two blog posts....

Andrew Markham | Sep 25, 2024 | Syndicated blog

Simple and Effective Personalization with Optimizely Data Platform (ODP)

As we dive into the amazing capabilities of Optimizely One, let’s shine a spotlight on the Optimizely Data Platform (ODP). This simple tool unifies...

Alex Harris - Perficient | Sep 24, 2024 | Syndicated blog

SaaS CMS - Self-hosting Presentation Layer with Coolify

Introduction In my previous post, I explored the hosting options for the Hybrid Headless/Fully Headless presentation layer (frontend) using Netlify...

Minesh Shah (Netcel) | Sep 23, 2024