6

The problem

The default/sample code in the LinqScratchPad.aspx shows a TryYourOwnClass class. Sadly, this class is not used in any way in the sample.

The sample code looks like this:

namespace Test {
    class Program {
        public static IEnumerable<SearchResultItem> Main(string str)
        {
            using (var context = ContentSearchManager.GetIndex("sitecore_master_index").CreateSearchContext())
            {
                return context.GetQueryable<SearchResultItem>().Take(10).ToList();
            }
        }
    }
}

[PredefinedQuery("_templatename", ComparisonType.Contains, "sample")]
public class TryYourOwnClass 
{
    [IndexField("_name")]
    public string Name {get; set;}

    [IgnoreIndexFieldAttribute]
    public string DoNotMapMe { get; set;}
}

As you can see, TryYourOwnClass is defined, but is never used. I tried using TryYourOwnClass as the type for GetQueryable, but that won't run, as the Main method returns an IEnumerable<SearchResultItem>. And I cannot change the return type, because that will break the script as well.

The question

So how do I use custom classes in the LinqScratchPad to query an index?

Why am I trying to do that?

I'm working on a pretty constrained environment, where we are using a custom index with custom fields. Sadly, I cannot install any third-party tools like "Luke" or something like that. That's why I would like to use LinqScratchPad and a custom class to query the index using all the fields I want.

  • 2
    did you try to inherit your class from SearchResultItem class? – Vlad Iobagiu Oct 20 '17 at 8:06
  • That totally did the trick. Thank you, @SitecoreClimber. This should be updated in the sample code. :-D – n-develop Oct 20 '17 at 8:08
  • I will transform my comment in an answer – Vlad Iobagiu Oct 20 '17 at 8:14
  • How to add custom fields to the result here? – Ankit Agrawal Jun 25 '18 at 14:56
  • Just add a new property to the "TryYourOwnClass" class and add the corresponding IndexField-attribute. That should do the trick, @AnkitAgrawal – n-develop Jun 25 '18 at 15:43
6

You need to inherits your custom class from SearchResultItem class if you want to return Queryable<SearchResultItem>

If you inherit from SearchResultItem, you will have all the goodies such as TemplateId, TemplateName, Name, CreatedBy, CreatedDate etc. Your code will look like:

namespace Test {
    class Program {
        public static IEnumerable<SearchResultItem> Main(string str)
        {
            using (var context = ContentSearchManager.GetIndex("sitecore_master_index").CreateSearchContext())
            {
                return context.GetQueryable<SearchResultItem>().Take(10).ToList();
            }
        }
    }
}

[PredefinedQuery("_templatename", ComparisonType.Contains, "sample")]
public class TryYourOwnClass : SearchResultItem
{
    [IndexField("_name")]
    public string Name {get; set;}

    [IgnoreIndexFieldAttribute]
    public string DoNotMapMe { get; set;}
}
  • That absolutely makes sense. I believe the output is limited to SearchResultItem, right? So I can use my custom class (and fields) for the query, but cannot see the custom fields in the result, correct? – n-develop Oct 20 '17 at 8:39
  • 1
    please decompile and have a look on this class : namespace Sitecore.Buckets.Client.sitecore.admin.LinqScratchPad – Vlad Iobagiu Oct 20 '17 at 8:52
  • 2
    I found out, that it is not limited to the properties of SearchResultItem. The script outputs all custom fields as well. Pretty cool. I'm starting to love this admin page. – n-develop Oct 20 '17 at 8:52

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.