Hi there,
thank you for your reply. I am at the end of my wits now. I feel I have tried everything and no amount of combinations leads to anything. Nice to be able to reach for help. Much appreciated. So... here is my code...
I have a DTO:
using Business.Dto;
public class LandingPageDto : IDto
{
public int DocumentID { get; set; }
public string Title { get; set; }
public string NodeAlias { get; set; }
}
I have a repository:
using Business.Repository;
using Business.Services.Query;
using System.Linq;
public class LandingPageRepository : BaseRepository, ILandingPageRepository
{
public LandingPageRepository(IDocumentQueryService documentQueryService) : base(documentQueryService)
{
}
public LandingPageDto GetLandingPage(string pageAlias)
{
return DocumentQueryService.GetDocument<CMS.DocumentEngine.Types.TSY.LandingPage>(pageAlias)
.AddColumns("LandingPageName")
.TopN(1)
.ToList()
.Select(landingPage => new LandingPageDto()
{
DocumentID = landingPage.DocumentID,
NodeAlias = landingPage.NodeAlias,
Title = landingPage.LandingPageName
})
.FirstOrDefault();
}
}
I have added NodeAlias to the DocumentQueryService like so:
using System;
using System.Linq;
using CMS.DocumentEngine;
using Business.Services.Context;
namespace Business.Services.Query
{
public class DocumentQueryService : BaseService, IDocumentQueryService
{
private ISiteContextService SiteContext { get; }
private readonly string[] _coreColumns =
{
// Defines initial columns returned for optimization. If not set, all columns are returned.
"NodeGUID", "DocumentID", "NodeID", "NodeAlias"
};
public DocumentQueryService(ISiteContextService siteContext)
{
SiteContext = siteContext;
}
public DocumentQuery<TDocument> GetDocument<TDocument>(Guid nodeGuid) where TDocument : TreeNode, new()
{
return GetDocuments<TDocument>()
.TopN(1)
.WhereEquals("NodeGUID", nodeGuid);
}
public DocumentQuery<TDocument> GetDocuments<TDocument>() where TDocument : TreeNode, new()
{
var query = DocumentHelper.GetDocuments<TDocument>();
// Loads the latest version of documents as preview mode is enabled
if (SiteContext.IsPreviewEnabled)
{
query = query
.Columns(_coreColumns.Concat(new[] { "NodeSiteId" })) // Sets initial columns returned for optimization.
//Adds 'NoteSiteD' column required for the Preview mode.
.OnSite(SiteContext.SiteName) // There could be more sites with matching documents
.LatestVersion()
.Published(false)
.Culture(SiteContext.PreviewCulture);
}
else
{
query = query
.Columns(_coreColumns) // Sets initial columns returned for optimization.
.OnSite(SiteContext.SiteName) // There could be more sites with matching documents
.Published()
.PublishedVersion()
.Culture(SiteContext.CurrentSiteCulture);
}
return query;
}
public DocumentQuery<TDocument> GetDocument<TDocument>(string pageAlias) where TDocument : TreeNode, new()
{
return GetDocuments<TDocument>()
.TopN(1)
.WhereEquals("NodeAlias", pageAlias);
}
}
}
Then my controller:
using Business.DependencyInjection;
using Kentico.PageBuilder.Web.Mvc;
using Kentico.Web.Mvc;
using System;
using System.Web.Mvc;
using ###.Controllers;
public class LandingPageController : BaseController
{
protected ILandingPageRepository LandingPageRepository { get; }
public LandingPageController(
IBusinessDependencies dependencies, ILandingPageRepository landingPageRepository) : base(dependencies)
{
LandingPageRepository = landingPageRepository ?? throw new ArgumentNullException(nameof(landingPageRepository));
}
// GET: LandingPage/[nodeAlias]
public ActionResult Index(string nodeAlias)
{
var landingPageDto = LandingPageRepository.GetLandingPage(nodeAlias);
//if (landingPageDto == null)
//{
// return HttpNotFound();
//}
var model = GetPageViewModel(landingPageDto.Title);
HttpContext.Kentico().PageBuilder().Initialize(landingPageDto.DocumentID);
return View(model);
}
}
And finally the view:
@using Kentico.PageBuilder.Web.Mvc
@using Kentico.Web.Mvc
<div class="section">
<div class="container">
<div class="kn-system-messages"></div>
@Html.Kentico().EditableArea("top")
</div>
</div>
@section Styles {
@Html.Kentico().PageBuilderStyles()
}
@section Scripts {
@Html.Kentico().PageBuilderScripts()
}
Any help will be much appreciated. I feel I am close but missing a vital bit... I am sure someone can help me to get to that "Eureka!" moment and help me to understand it all better.
Thanks a lot. It is really appreciated.