Hi,
Change: x.YYYBody.ToString() => x.YYYBody.AsViewedByAnonymous() in the Select statements.
/Henrik
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.
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
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. :-)
Ah! Thanks Magnus. The hack works.
I'm going for the AsCropped solution which does the trick :)
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: