November Happy Hour will be moved to Thursday December 5th.

How to Find Parentpage of a subpage

Vote:
 

I have a issue of finding parent page of a subpage. 

My scenerio:

I have BankPage as parent, which contain branchPage (which is children). 

I find all the branch pages by following

private EPiServer.Find.SearchResults<Filial> GetAllBranchPages()
{
var results = client.Search<Totalkredit.EpiServer.Models.Pages.BranchPage>()
.Select(x => new Filial
{
Coordinates = x.Coordinates,
StreetAddress = x.Street,
Latitude = x.Latitude,
Longitude = x.Longitude,
ZipCode = x.ZipCode,
AdvisorName = x.AdvisorName,
Email = x.Email,
Telefon = x.Telefon,
City = x.City,
AdvisorTitle = x.AdvisorTitle,
ParentBankPage = GetBankById(x.ParentLink.ID)

})
.GetResult();

return results;
}

Above code work fine except this line: ParentBankPage = GetBankById(x.ParentLink.ID)

i have a function which is suppose to return the parent Bank page

private EPiServer.Find.SearchResults<BankPage> GetBankById(int id)
{

EPiServer.Find.Api.Querying.Filters.IdsFilter x = new EPiServer.Find.Api.Querying.Filters.IdsFilter(new List<string>{id.Tostring()}.ToArray());

var results = client.Search<Totalkredit.EpiServer.Models.Pages.BankPage>()
.Filter(x)
.Select(y => new BankPage
{
BankName = y.BankName,
BackGroundcolor = y.BackGroundcolor,
Logo = y.Logo,
})
.GetResult();
return results;
}

it return 0 Hits.

 

 

#73242
Jul 11, 2013 13:32
Vote:
 

Why not just use the EPiServer CMS API to get the parent page? contentRepository.Get<BankPage>(x.ParentLink);

Frederik

#73245
Jul 11, 2013 13:55
Vote:
 

Thanks for reply Frederik.

I have changed the function as follows

// Get all BranchPages in episerver
private EPiServer.Find.SearchResults<Filial> GetAllBranchPages()
{
var repository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();

var results = client.Search<Totalkredit.EpiServer.Models.Pages.BranchPage>()
.Select(x => new Filial
{
Coordinates = x.Coordinates,
StreetAddress = x.Street,
Latitude = x.Latitude,
Longitude = x.Longitude,
ZipCode = x.ZipCode,
AdvisorName = x.AdvisorName,
Email = x.Email,
Telefon = x.Telefon,
City = x.City,
AdvisorTitle = x.AdvisorTitle,
ParentBankPage = repository.Get<BankPage>(x.ParentLink),
BankName = repository.Get<BankPage>(x.ParentLink).BankName
})
.GetResult();

return results;
}

These to lines together in above function

ParentBankPage = repository.Get<BankPage>(x.ParentLink),
BankName = repository.Get<BankPage>(x.ParentLink).BankName  

throws 

{"Sequence contains more than one matching element"}

if i remove BankName = repository.Get<BankPage>(x.ParentLink).BankName  

so it only contains ParentBankPage = repository.Get<BankPage>(x.ParentLink)

throws : An exception of type JsonSerializationException was thrown while deserializing object.

Filial class in above function is defined as follows

public class Filial
{
public Filial()
{
}
public string ZipCode { get; set; }
public string AdvisorTitle { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public string StreetAddress { get; set; }
public GeoCoordinate Coordinates { get; set; }
public BankPage ParentBankPage { get; set; }
public string AdvisorName { get; set; }
public string BankName { get; set; }
public string Email { get; set; }
public string Telefon { get; set; }
public string City { get; set; }
}

 

#73253
Jul 11, 2013 15:30
* 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.