Categories
Bloggers
Blogs RSS feed

Adding Custom Search Results into Sitefinity Search Results Widget

by Stanislav Velikov
The Sitefinity search results widget renders search results for content that is saved in Sitefinity search index. In order to make the search result widget render a result which is not located in Sitefinity search, the search result can be added on the fly while searching.

The data to construct this custom search result can be coming from a custom query.
To create this customization, create custom search results widget.

Here is the complete sample code.

The customization follows the approach from the blog post for trimming search results based on permissions.

The custom result added is of type IDocument which have several properties to be populated to render complete search result ID, Title, Content, Summary, Link and HighlightedResult. Title and Content.

//add custom search result
                    var searchService = this.GetService();
                    IList<IField> fields = new List<IField>(4);
                    IField fld1 = searchService.CreateDocumentField("Title", "title");
                    IField fld2 = searchService.CreateDocumentField("Id", "Identifier");
                    IField fld3 = searchService.CreateDocumentField("Content", "content");
                    IField fld4 = searchService.CreateDocumentField("Summary", "");
                    IField fld5 = searchService.CreateDocumentField("Link", "http://google.com");
                    IField fld6 = searchService.CreateDocumentField("HighLighterResult", "this is the higjligthed result");
 
                    fields.Add(fld1); fields.Add(fld2); fields.Add(fld3); fields.Add(fld4); fields.Add(fld5); fields.Add(fld6);
 
                    var document = searchService.CreateDocument(fields, "Id");
                    var res = result.ToList();
                    res.Add(document);
                    this.ResultsList.DataSource = res;

The attached sample can be used to automatically create search result on the fly based on the queries that the user enters when searching the website.

Leave a comment