private void PopulateDefaultContent(RichHtmlPlaceholderControl titlePlaceholder, PlaceHolder sectionContent, bool useLinks, Control fromHint) { // 1. Parse a URL out of the title placeholder var match = Regex.Match(titlePlaceholder.Html, "href=\"(?<URL>[^\"]+)\"[^>]*>(?<Subject>.*?)</a>"); if (!match.Success) { return; } // 2. Get that URL as a CMS posting var path = match.Groups["URL"].Value; // Reject anchor links because it's the current page, and because Umbraco throws an exception looking it up if (path.StartsWith("#", StringComparison.Ordinal)) { return; } var cmsPath = path; if (cmsPath.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) { cmsPath = cmsPath.Substring(6); // turn "http://yourcouncil/" into "/yourcouncil" } if (cmsPath.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) { cmsPath = cmsPath.Substring(6); // turn "http://yourcouncil/" into "/yourcouncil" } if (!cmsPath.StartsWith("/", StringComparison.OrdinalIgnoreCase)) { try { cmsPath = Page.ResolveUrl(cmsPath); } catch (HttpException ex) { // Catch an invalid URL which could not be resolved, such as ../ trying to go beyond the root folder. // Report the error to be fixed, but we don't want the whole page to fail. ex.Data.Add("URL which failed", cmsPath); ex.ToExceptionless().Submit(); return; } } // Remove domain if it looks like the same site, as Umbraco throws an exception when looking it up var pathAsUri = new Uri(cmsPath, UriKind.RelativeOrAbsolute); if (pathAsUri.IsAbsoluteUri && (pathAsUri.Host.EndsWith(".eastsussex.gov.uk") || pathAsUri.Host.EndsWith(".azurewebsites.net")) ) { cmsPath = pathAsUri.AbsolutePath; } var from = String.Empty; var posting = UmbracoContext.Current.ContentCache.GetByRoute(cmsPath); if (posting != null) { from = "from linked page"; } else { // If the link was the default posting in a Microsoft CMS channel, we need to remove the page name and get the // 'channel' name to match the way it was imported as an Umbraco page cmsPath = cmsPath.TrimEnd('/'); var pos = cmsPath.LastIndexOf("/", StringComparison.Ordinal); if (pos > 0) { cmsPath = cmsPath.Substring(0, pos); posting = UmbracoContext.Current.ContentCache.GetByRoute(cmsPath); if (posting != null) { from = "from parent of linked page"; } } } if (posting != null) { // 4. Get the content from the destination page and display it if (useLinks) { // 4a. If destination is a landing page or landing page with box, find first 5 links var links = new List <string>(5); if (posting.DocumentTypeAlias == "standardLandingPage") { var i = 1; const int max = 15; while (links.Count < 5 && i <= max) { var link = posting.GetPropertyValue <string>("phDefListTitle" + i.ToString("00", CultureInfo.InvariantCulture) + "_Content").Trim(); if (!String.IsNullOrEmpty(link)) { // Strip any extra tags around the link (typically <p></p>) var pos = link.IndexOf("<a ", StringComparison.OrdinalIgnoreCase); if (pos > 0) { link = link.Substring(pos); } pos = link.LastIndexOf("</a>", StringComparison.OrdinalIgnoreCase); if (pos > -1) { link = link.Substring(0, pos + 4); } links.Add(link); } i++; } } // 5a. If we found some links, display all 4, or first 3 with "more" link if (links.Count > 0) { var list = new StringBuilder("<ul>"); for (var i = 0; i < links.Count; i++) { if (i == 3 && links.Count != 4) { break; } list.Append("<li>").Append(links[i]).Append("</li>"); } if (links.Count == 5) { list.Append("<li><a href=\"").Append(CmsUtilities.CorrectPublishedUrl(path)).Append("\">More on " + match.Groups["Subject"].Value + "</a></li>"); } list.Append("</ul>"); sectionContent.Controls.Add(new LiteralControl(list.ToString())); from = "First few links " + from; } else { useLinks = false; } } if (!useLinks) { // 4b. Otherwise use the page description sectionContent.Controls.Add(new LiteralControl("<p class=\"medium large\">" + HttpUtility.HtmlEncode(posting.GetPropertyValue <string>("pageDescription")) + "</p>")); from = "Description " + from; } } if (!String.IsNullOrEmpty(from)) { fromHint.Visible = true; fromHint.Controls.Clear(); fromHint.Controls.Add(new LiteralControl(from)); } else { fromHint.Visible = false; } }