4

I have the following POCO class:

using Sitecore.ContentSearch;
using Sitecore.ContentSearch.SearchTypes;
using Sitecore.Web.UI.WebControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace kgSitecoreTraining.Models
{

public class BlogItem:SearchResultItem

{
    public BlogItem() { }

    public string  Author {
        get {
             GetItem().Statistics.UpdatedBy.ToString();
        }
    }

    [IndexField("AuthorImage")]
    public HtmlString AuthorImage {
        get{
               return new HtmlString(FieldRenderer.Render(GetItem(), "AuthorImage", "DisableWebEditing=true"));
           }            
    }

    [IndexField("Title")]
    public string Title { get; set; }

    [IndexField("Subtitle")]
    public string Subtitle { get; set; }

    new public string CreatedDate { get; set; }
    public string URL{ get; set; }

 }
}

In the Author property I wanted to access the statistics property but it gave me this error: enter image description here SO in this POCO class I cant access the Statistics property, but if I modify the Author property to just a get;set; and then use it like this, it works:

var currentItem = RenderingContext.Current.ContextItem;
            var databaseName = currentItem.Database.Name.ToLower();
            var indexName = string.Format("kgsite_{0}_index", databaseName);
            var index = ContentSearchManager.GetIndex(indexName);
            var model = new BlogItemList();

            using (var context = index.CreateSearchContext()) {
                var results = context.GetQueryable<BlogItem>().Where(i => i.Paths.Contains(currentItem.ID)).GetResults();
                model.Blogs = results.Hits.Select(h => new BlogItem() {
                    Title = h.Document.Title,
                    Subtitle = h.Document.Subtitle,
                    CreatedDate = h.Document.CreatedDate,
                    URL = h.Document.Url,
                   Author =  h.Document.GetItem().Statistics.UpdatedBy.ToString()

                }).ToList();
                model.TotalResultCount = results.TotalSearchResults;              
            }

So why this works and the other one doesnt?

1
4

First of all you don't have to use GetItem() method.

When you used GetItem it's get the value from Sitecore Database not from index and you will have big performance problem if you query thousands of items.

Can you try in this way ?

public string  Author {
        get {
             base.UpdatedBy.ToString();
        }
    }
4
  • That's not working at all :/ Now it shows this error: Error Oct 9 '17 at 14:42
  • Could it be something wrong with the index files I created? Oct 9 '17 at 14:45
  • Can you try to reindex your index and to verify it with Luke ? Oct 9 '17 at 14:56
  • Hey, I created it! And the files are indeed being indexed. Oct 9 '17 at 16:08
1

Looking at the error, it's because it can't get the item on GetItem() for some reason.

Possibly a context issue. I would decompile in to that method to see how it's generating the key and getting the item compared to the h.Document.GetItem() method which isn't working.

Hunch is that it's something to do with which database you're in, maybe the item isn't published and you're getting it from master and the context item is web. Or it could be a bug and the collection used by GetItem() isn't being populated.

Anyway, it's a good idea to look in to the code to see.

What version of Sitecore are you on?

2
0

The thing was that I overwrote the index, so in the default one there were fields needed for SearchResultsItem properties to be populated. As I overwrote the index with my own I didn't added those fields (and computed fields such as _smallcreateddate), so I just added them to my index and it works now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.