-
Notifications
You must be signed in to change notification settings - Fork 6
Sitecore search with GlassMapper
cardinal252 edited this page Oct 24, 2013
·
7 revisions
Ok, so here for me is the crowning glory of lucinq and sitecore - using search to get POCO objects. I am using the GlassMapper since its common and reasonably mature.
I set up the following class:
[SitecoreType(AutoMap = true)]
public class PageContent
{
[SitecoreId]
public Guid Id { get; set; }
[SitecoreField("Page Title")]
public string PageTitle { get; set; }
[SitecoreField("Page Subtitle")]
public string PageSubtitle { get; set; }
}
Then used the following code to query and return results from the sitecore database.
// assuming glass is already set up
ISitecoreService service = new SitecoreService("web");
LuceneSearch search = new LuceneSearch("path to my index");
// get a normal sitecore query builder
ISitecoreQueryBuilder queryBuilder = new SitecoreQueryBuilder();
// use one of the glass mapper extensions to setup the query
queryBuilder.Field<PageContent>(t => t.PageTitle, "*new*");
// get the search result
var sitecoreSearchResult = new GlassSearchResult(service, search.Execute(queryBuilder));
// do something with it
var result = sitecoreSearchResult.GetPagedItems<PageContent>(0, 20);
// as always you can also use the IEnumerable<PageContent>
foreach (PageContent item in result)
{
Console.WriteLine("Page Title: " + item.PageTitle);
Console.WriteLine("Page Subtitle: " + item.PageSubtitle);
Console.WriteLine("Id: " + item.Id);
}