/// <summary> /// Gets a list of `SubNav` items under the `Node` passed in. /// </summary> /// <param name="node"></param> /// <returns></returns> public IEnumerable <SubNav> GetSubNavFromNode(ITreeNode node) { var subnavList = new List <SubNav>(); try { foreach (ITreeNode Node in DocumentQueryHelper.RepeaterQuery( Path: node.NodeAliasPath + "/%", ClassNames: "CMS.MenuItem", OrderBy: "NodeLevel, NodeOrder", Columns: "MenuItemName,NodeAliasPath" )) { subnavList.Add(new SubNav() { LinkText = Node.GetValue("MenuItemName").ToString(), // You have to decide what your URL will be, for us our URLs = NodeAliasPath LinkUrl = Node.NodeAliasPath }); } } catch (Exception ex) { _eventLogService.LogException("ExampleService", "GET", ex); } return(subnavList); }
/// <summary> /// Simply gets the current node using the absolute path of the HttpContext. /// </summary> /// <param name="context"></param> /// <returns></returns> public ITreeNode GetCurrentNode() { var absolutePath = _httpContextAccessor.HttpContext.Request.Url.AbsolutePath; ITreeNode FoundNode = DocumentQueryHelper.GetNodeByAliasPath(absolutePath); return(FoundNode); }
/// <summary> /// Example of how to do Web Parts in MVC Using RenderAction, and in this case the parent Controller is filling a model /// that will be passed to the "Web Part" (Action) in order to render. More leg work but also source-agnostic. (Model-View-ViewModel MVVM) /// </summary> /// <returns></returns> public ActionResult MVCWebPartsViewModel() { TreeNode FoundNode = DocumentQueryHelper.GetNodeByAliasPath(HttpContext.Request.Url.AbsolutePath); if (FoundNode != null) { ExampleMVCWebPartsViewModel Model = new ExampleMVCWebPartsViewModel(); // Get the Sub Nav Items foreach (TreeNode Node in DocumentQueryHelper.RepeaterQuery( Path: FoundNode.NodeAliasPath + "/%", ClassNames: "CMS.MenuItem", OrderBy: "NodeLevel, NodeOrder", Columns: "MenuItemName,NodeAliasPath" )) { Model.SubNavigation.Add(new SubNav() { LinkText = Node.GetValue("MenuItemName", ""), // You have to decide what your URL will be, for us our URLs = NodeAliasPath LinkUrl = Node.NodeAliasPath }); } return(View(Model)); } else { return(HttpNotFound("Could not find page by that Url")); } }
/// <summary> /// This Repeater "Web Part" is given the ViewBag (since it's normally not available on child actions) and configures itself from this. /// This allows for easy calling and adjustment to this "Web Parts" Logic without really needing adjustments on the calling Views, but /// is very much tied into the source (ex: Kentico). It would be hard to flip this with Kentico Cloud if you wished to use that. /// </summary> /// <param name="ViewBag">The ViewBag which will have the Document, Culture, and Site Context</param> /// <returns></returns> public ActionResult NavigationByContext(dynamic ViewBag) { // Build the actual Partial View's model from the data provided by the parent View ExampleMVCWebPartsSubNavs Model = new ExampleMVCWebPartsSubNavs(); var subNavs = new List <SubNav>(); // Get the Sub Nav Items foreach (ITreeNode Node in DocumentQueryHelper.RepeaterQuery( Path: ViewBag.CurrentDocument.NodeAliasPath + "/%", CultureCode: ((CultureInfo)ViewBag.CurrentCulture).CultureCode, SiteName: ((ISiteInfo)ViewBag.CurrentSite).SiteName, ClassNames: "CMS.MenuItem", OrderBy: "NodeLevel, NodeOrder", Columns: "MenuItemName,NodeAliasPath" )) { subNavs.Add(new SubNav() { LinkText = Node.GetValue("MenuItemName").ToString(), // You have to decide what your URL will be, for us our URLs = NodeAliasPath LinkUrl = Node.NodeAliasPath }); } Model.SubNavigation = subNavs; return(View("Navigation", Model)); }
public ActionResult Banners() { TreeNode FoundNode = DocumentQueryHelper.GetNodeByAliasPath(HttpContext.Request.Url.AbsolutePath); if (FoundNode != null) { SetContext(FoundNode.DocumentID); // Get Banners ExampleBannersViewModel Model = new ExampleBannersViewModel(); Model.BannerNameUrlsList = new List <ExampleBannersBanner>(); foreach (TreeNode Banner in DocumentQueryHelper.RepeaterQuery(ClassNames: "Demo.Banner", RelationshipName: "Banners", RelationshipWithNodeGuid: FoundNode.NodeGUID)) { Model.BannerNameUrlsList.Add(new ExampleBannersBanner() { BannerName = Banner.GetValue <string>("BannerName", ""), BannerUrl = Banner.GetValue <string>("BannerImage", "") }); } return(View(Model)); } else { return(HttpNotFound("Could not find page by that Url")); } }
/// <summary> /// This Repeater "Webpart" Relies on just a path that would be provided through the View's context. Does not rely on passing /// the ViewBag like the NavigationByContext, but does then require the calling View to provide the properties, and if ever more /// properties are needed, would need to adjust both Controller and View alike. /// </summary> /// <returns></returns> public ActionResult NavigationByPath(string Path, string Culture, string SiteName) { // Build the actual Partial View's model from the data provided by the parent View ExampleMVCWebPartsSubNavs Model = new ExampleMVCWebPartsSubNavs(); List <SubNav> SubNavs = new List <SubNav>(); // Get the Sub Nav Items foreach (TreeNode Node in DocumentQueryHelper.RepeaterQuery( Path: Path + "/%", CultureCode: Culture, SiteName: SiteName, ClassNames: "CMS.MenuItem", OrderBy: "NodeLevel, NodeOrder", Columns: "MenuItemName,NodeAliasPath" )) { Model.SubNavigation.Add(new SubNav() { LinkText = Node.GetValue("MenuItemName", ""), // You have to decide what your URL will be, for us our URLs = NodeAliasPath LinkUrl = Node.NodeAliasPath }); } return(View("Navigation", Model)); }
// GET: Home public ActionResult Index() { // Get the current page, in this case we know it's the Home page var HomePage = DocumentQueryHelper.RepeaterQuery(Path: "/Home", ClassNames: "MVC.Home", CacheItemName: "HomePage").GetTypedResult().Items.FirstOrDefault(); HttpContext.Kentico().PageBuilder().Initialize(HomePage.DocumentID); SetContext(HomePage.DocumentID); return(View()); }
public static IDataReader GetDocument(Int32 docId) { try { return(DocumentQueryHelper.GetDocument(docId)); } catch (SqlException sqlEx) { Console.WriteLine(sqlEx.ToString()); throw new DataException("An exception occured getting the document.", sqlEx); } }
// GET: GenericWidgetPage public ActionResult Index() { GenericWidgetPage FoundNode = (GenericWidgetPage)DocumentQueryHelper.GetNodeByAliasPath(EnvironmentHelper.GetUrl(HttpContext.Request), GenericWidgetPage.OBJECT_TYPE); if (FoundNode != null) { SetContext(FoundNode.DocumentID); return(View(FoundNode.Layout)); } else { return(HttpNotFound("Could not find page by that Url")); } }
public ActionResult UnregisteredTemplate() { TreeNode FoundNode = DocumentQueryHelper.GetNodeByAliasPath(EnvironmentHelper.GetUrl(HttpContext.Request)); if (FoundNode != null) { HttpContext.Kentico().PageBuilder().Initialize(FoundNode.DocumentID); return(View()); } else { return(new HttpNotFoundResult()); } }
/// <summary> /// Gets a list of `SubNav` items under the `Node` passed in. /// </summary> /// <param name="node"></param> /// <returns></returns> public IEnumerable <SubNav> GetSubNavFromAliasPath(string nodeAliasPath, CultureInfo cultureInfo = null, ISiteInfo siteInfo = null) { if (siteInfo == null) { // If site is not provided, get the current site siteInfo = _siteService.CurrentSite; _eventLogService.LogEvent("GetSubNavFromAliasPath", "ExampleService", "GET", "Using current site"); } else { _eventLogService.LogEvent("GetSubNavFromAliasPath", "ExampleService", "GET", "Using passed in site"); } if (cultureInfo == null) { cultureInfo = LocalizationContext.GetCurrentCulture(); _eventLogService.LogEvent("GetSubNavFromAliasPath", "ExampleService", "GET", "Using current culture"); } else { _eventLogService.LogEvent("GetSubNavFromAliasPath", "ExampleService", "GET", "Using passed in culture"); } var subnavList = new List <SubNav>(); try { foreach (ITreeNode Node in DocumentQueryHelper.RepeaterQuery( Path: nodeAliasPath + "/%", CultureCode: cultureInfo.CultureCode, SiteName: siteInfo.SiteName, ClassNames: "CMS.MenuItem", OrderBy: "NodeLevel, NodeOrder", Columns: "MenuItemName,NodeAliasPath" )) { subnavList.Add(new SubNav() { LinkText = Node.GetValue("MenuItemName").ToString(), // You have to decide what your URL will be, for us our URLs = NodeAliasPath LinkUrl = Node.NodeAliasPath }); } } catch (Exception ex) { _eventLogService.LogException("ExampleService", "GET", ex); } return(subnavList); }
/// <summary> /// Example of how to do Web Parts in MVC using RenderAction, and in this case no model is provided for the webpart, just using /// the ViewBag Context set by SetContext, similar to in Portal Method how you would use Macros. /// </summary> /// <returns></returns> public ActionResult MVCWebPartsNoModel() { TreeNode FoundNode = DocumentQueryHelper.GetNodeByAliasPath(HttpContext.Request.Url.AbsolutePath); if (FoundNode != null) { // This will give us the Node Alias Path context SetContext(FoundNode.DocumentID); return(View()); } else { return(HttpNotFound("Could not find page by that Url")); } }
// GET: HomePageTemplates public ActionResult Index() { // Retrieves the page from the Kentico database TreeNode page = DocumentQueryHelper.GetNodeByAliasPath(EnvironmentHelper.GetUrl(HttpContext.Request)); // Returns a 404 error when the retrieving is unsuccessful if (page == null) { return(HttpNotFound()); } // Generate your own view model. TemplatedPageCustomControllerViewModel Model = new TemplatedPageCustomControllerViewModel() { Message = "Hello World! Custom Message" }; // Have to hard code as the normal Return View() still has routing context of Home page controller return(View("PageTemplates/_TemplatedPageCustomController", Model)); }
/// <summary> /// Gets a list of `Demo.Banner` items under the `Node` passed in. /// </summary> /// <param name="node"></param> /// <returns></returns> public IEnumerable <ExampleBannersBanner> GetBannersFromNode(ITreeNode node) { var bannerList = new List <ExampleBannersBanner>(); try { foreach (ITreeNode Banner in DocumentQueryHelper.RepeaterQuery(ClassNames: "Demo.Banner", RelationshipName: "Banners", RelationshipWithNodeGuid: node.NodeGUID)) { bannerList.Add(new ExampleBannersBanner() { BannerName = Banner.GetValue("BannerName").ToString(), BannerUrl = Banner.GetValue("BannerImage").ToString() }); } } catch (Exception ex) { _eventLogService.LogException("ExampleService", "GET", ex); } return(bannerList); }
public override void WriteTo(StringBuilder writer) { writer.Append("moreLikeThis("); if (DocumentParameterName == null) { if (WhereTokens.Count > 0) { var token = WhereTokens.First; while (token != null) { DocumentQueryHelper.AddSpaceIfNeeded(token.Previous?.Value, token.Value, writer); token.Value.WriteTo(writer); token = token.Next; } } else { writer.Append("true"); } } else { writer.Append("$"); writer.Append(DocumentParameterName); } if (OptionsParameterName == null) { writer.Append(")"); return; } writer.Append(", $"); writer.Append(OptionsParameterName); writer.Append(")"); }