Dynamically Replacing Content in Kentico - Part 2

   —   

Dynamically replacing content within a site can improve content production and ensure consistency for organizations. While this a great solution for companies looking to expedite their content editing, external links should be verified to ensure the new content is correct and valid. In this blog, I’ll show you how to build upon my original blog and add a validation step to ensure your external links are valid.

In my previous post on the topic, I demonstrated how to use Custom Tables and Global Event Handlers to update content dynamically when it is sent to the browser. This is a great solution for companies looking to add some dynamic capabilities to their site and ensure consistency throughout their content. It is important, however, to ensure the replacement content is valid and correct. In this blog, I’ll show you how you can quickly add this capability to your application.\

Add logging fields

The first step of the process is to add two new fields to the custom table containing my replacement content. These fields were:

Validated (bool)
This is a true or false field indicating whether the ReplacementURL is a valid resource.

LastValidated (datetime)
This is a date and time field indicating the last time the ReplacementURL was validated.

With these two fields added to the table, I was ready to implement my validation functionality.

Create a scheduled task to validate the content

The next step is to create a new scheduled task to validate the content. After creating the initial task, I added the following functionality to validate each RepalcementURL value:

// Get the content replacement links that have not been validated in the last 7 days var items = CustomTableItemProvider.GetItems("Custom.ContentURLReplacementTable") .WhereGreaterThan("lastValidated", DateTime.Now.AddDays(-7)); foreach (var item in items) { // Validate the replacement url HttpWebRequest request = WebRequest.Create(item.GetStringValue("ReplacementURL", "")) as HttpWebRequest; request.AllowAutoRedirect = false; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { if (response.StatusCode == HttpStatusCode.OK) { // Set the record as valid and add the timestamp item.SetValue("Validated", true); item.SetValue("LastValidated", DateTime.Now); item.Update(); intTotalValidRecords += 1; } else { // Update the custom table record item.SetValue("Validated", false); item.Update(); intTotalInvalidRecords += 1; } };

I only returned the records that had not been validated within the last seven days to minimize the impact to the system. I used the HttpWebRequest API to verify that the ReplacementURL was valid. If the response is valid (OK or 200), then the custom table record is updated with the current date and time.

IMPORTANT NOTE

When a URL is not reachable, an NXDOMAIN response is returned to the client’s browser. Some ISPs may intercept these responses and redirect the client to a different page or domain. In this case, the HttpStatusCode will always be OK (200). The above code will always mark each record as valid based on the response. If you experience this behavior, contact your ISP regarding filtering of NXDOMAIN responses.

Update the global event handler

After adding the scheduled task, the global event handler needs to be updated to leverage the new fields. In the OnAfterFiltering event, I added the following code to make sure the record had been validated:

// Determine if the replacement URL is valid // This is updated byt he ContentReplacementValidationTask task if (item.GetBooleanValue("Validated", false)) {

This check ensures that only valid links are processed and replaced in the output.

Creating a report

The last step is to create a simple report containing all the invalid links (named Content Replacement URL - Invalid Records Report). In a real scenario, an administrator could subscribe to this report on a daily or even weekly basis and investigate any invalid links and resolve them.

SELECT OriginalContent AS 'Origianl Content' ,Validated ,LastValidated AS 'Last Validated' FROM Custom_ContentURLReplacementTable WHERE Validated = 0

Testing

To test the validation functionality, I updated a record with an invalid link and executed the task. This resulted in the record being marked as Validated = false.

Invalid Record

I then accessed the site with the invalid record and confirmed the text had not been dynamically replaced.

Dynamic Content Page

Lastly, I checked the Content Replacement URL - Invalid Records Report to confirm the invalid record was listed.

Report 1

Moving Forward

Dynamically replacing content is a great way to add links and additional information to your site on an automated basis. Always be sure to check the content you’re adding to ensure it is valid and correct. By using the above solution, the scheduled task could be executed once a day to confirm the replacement URLs are correct and valid. Good luck!

Get the code

Get the task

This blog is intended for informational purposes only and provides an example of one of the many ways to accomplish the described task. Always consult Kentico Documentation for the best practices and additional examples that may be more effective in your specific situation.

Share this article on   LinkedIn

Bryan Soltis

Hello. I am a Technical Evangelist here at Kentico and will be helping the technical community by providing guidance and best practices for all areas of the product. I might also do some karaoke. We'll see how the night goes...