XhtmlString (MainBody) is null. How do I get the value?

Vote:
 

How come description is null? If I try to get the AsViewedByAnonymous I get an empty string. The text is clearly there, I just cant figure out how to get it!

The search:

public JsonResult Search(string searchString)
{

var allResults = SearchClient.Instance.Search<FactPage>().For(searchString)
.Filter(x => x.RolesWithReadAccess().Match("Everyone"))
.Select(x => new SearchHit
{
Heading = x.Name,
Type = "facts",
Description = x.MainBody.ToString(),
LinkUrl = x.LinkURL
})
.IncludeType<SearchHit, LessonPage>(x =>
new SearchHit
{
Heading = x.Name,
Type = "lesson",
Description = x.BodyOne.ToString(),
LinkUrl = x.LinkURL
})
.IncludeType<SearchHit, VideoBlock>(x =>
new SearchHit
{
Heading = x.H2Text,
Type = "videoblock",
Description = x.Description,
LinkUrl = x.VideoFileUrl.ToString()
})
.Track()
.Take(10)
.GetResult();


var results = new
{
all = allResults
};

return Json(results, JsonRequestBehavior.AllowGet);
}

 

 

The indexed Item(from find explorer)

Källdokument:

{
  "$type": "WebProject.Models.Pages.LessonPage, WebProject",
  "BodyOne": {
    "$type": "EPiServer.Core.XhtmlString, EPiServer",
    "IsEmpty$$bool": false,
    "IsModified$$bool": false,
    "___types": [
      "EPiServer.Core.XhtmlString",
      "System.Object",
      "System.Web.IHtmlString",
      "System.Runtime.Serialization.ISerializable",
      "EPiServer.Data.Entity.IReadOnly`1[[EPiServer.Core.XhtmlString, EPiServer, Version=7.5.1002.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7]]",
      "EPiServer.Data.Entity.IReadOnly"
    ],
    "AsViewedByAnonymous$$string": "Lamm är ett riktigt mört kött. Hemligheten för att få ett riktigt saftigt kött är att använda en stektermometer och inte laga på för hög värme. Förutom massa recept på saftiga lammrätter, hittar du också tips på hur du tillagar lammköttet på bästa sätt."
  },
#81635
Feb 21, 2014 10:30
Vote:
 

Hi,

Change: x.YYYBody.ToString() => x.YYYBody.AsViewedByAnonymous() in the Select statements.

/Henrik

#81647
Feb 21, 2014 16:47
Vote:
 

Thanks Henrik (I had the ToString() method which I tried before posting to forum). The following example returns an empty text string using AsViewedByAnonymous().

public JsonResult Search(string searchString)
{var allResults = SearchClient.Instance.Search<FactPage>().For(searchString)
.Filter(x => x.RolesWithReadAccess().Match("Everyone"))
.Select(x => new SearchHit
{
Heading = x.Name,
Type = "facts",
Description = x.MainBody.AsViewedByAnonymous(),
LinkUrl = x.LinkURL
})
.IncludeType<SearchHit, LessonPage>(x =>
new SearchHit
{
Heading = x.Name,
Type = "lesson",
Description = x.BodyOne.AsViewedByAnonymous(),
LinkUrl = x.LinkURL
})
.IncludeType<SearchHit, VideoBlock>(x =>
new SearchHit
{
Heading = x.H2Text,
Type = "videoblock",
Description = x.Description,
LinkUrl = x.VideoFileUrl.ToString()
})
.Track()
.Take(10)
.GetResult();
var results = new
{
all = allResults
};return Json(results, JsonRequestBehavior.AllowGet);
}

 I have tried to reindex the site as well. 

 

 

#81649
Edited, Feb 21, 2014 16:58
Vote:
 

Sometimes I get a first chance exception instead of empty string.. it looks like this. Is there a chance the AsViewedByAnonymous() returns something else than a string? 

A first chance exception of type 'EPiServer.Find.ProjectionException' occurred in EPiServer.Find.dll

Additional information: Invalid operation while projecting search result item to type SearchHit with expression x => new SearchHit() {Heading = x.Name, Type = "lesson", Description = x.BodyOne.AsViewedByAnonymous(), LinkUrl = x.LinkURL}. The fields to bind were ___types, Name$$string, LinkURL$$string, MainBody. variable 'x' of type 'WebProject.Models.Pages.LessonPage' referenced from scope '', but it is not defined

#81650
Feb 21, 2014 18:14
Vote:
 

x.YYYBody.AsViewedByAnonymous() should have worked, but it seems like there is an indifference in the field value specification. 

To verify, you could try this super nasty hack (not recommended):

    public class Hack
    {
        public HackBody MainBody { get; set; }
        public class HackBody
        {
            public string AsViewedByAnonymous { get; set; }
        }
    }   

    

and then

Description = ((Hack)(object)x).MainBody.AsViewedByAnonymous,

However, an easy fix to this would probably be to use x.YYYBody.AsCropped(int.MaxValue), unless Henrik has a clever trick up his sleeve. :-)


#81658
Edited, Feb 23, 2014 11:40
Vote:
 

Ah! Thanks Magnus. The hack works.

I'm going for the AsCropped solution which does the trick :)

#81660
Feb 23, 2014 13:04
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.