Lee Crowe
Nov 19, 2014
  3960
(0 votes)

Content search and replace

The development team I currently work in like to have content in line with production content but it very quickly becomes outdated.


To aid with this we have Go pipelines which restore production database backups over lower internal environment databases.


The CMS Site has many links to various other product journeys. These journeys have environment specific Urls. If we are are in our regression environment, we would like content links to hand off to the relevant regression product journeys.


Our go pipeline restores the production database and performs many content replacements by executing a search and replace stored procedure. Below you will find the stored procedure definition to search and replace content.


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_CustomSearchAndReplace]') AND type in (N'P', N'PC'))
      DROP PROCEDURE [dbo].[usp_CustomSearchAndReplace]
GO

CREATE PROCEDURE [dbo].[usp_CustomSearchAndReplace]
      @SearchFor nvarchar(1000),
      @ReplaceWith nvarchar(1000) = ''
AS
      SELECT * FROM [tblContentProperty] WHERE [String] LIKE '%' + @SearchFor + '%' OR [LongString] LIKE '%' + @SearchFor + '%'
      SELECT * FROM [tblWorkContentProperty] WHERE [String] LIKE '%' + @SearchFor + '%' OR [LongString] LIKE '%' + @SearchFor + '%'

      IF LEN(LTRIM(RTRIM(@ReplaceWith))) > 0
      BEGIN
            PRINT 'Updating published pages'
            UPDATE [tblContentProperty] SET [String] = REPLACE([String], @SearchFor, @ReplaceWith) WHERE [String] LIKE '%' + @SearchFor + '%'
            UPDATE [tblContentProperty] SET [LongString] = REPLACE(CAST([LongString] AS nvarchar(max)), @SearchFor, @ReplaceWith) WHERE [LongString] LIKE '%' + @SearchFor + '%'
            PRINT 'Updating unpublished pages'
            UPDATE [tblWorkContentProperty] SET [String] = REPLACE([String], @SearchFor, @ReplaceWith) WHERE [String] LIKE '%' + @SearchFor + '%'
            UPDATE [tblWorkContentProperty] SET [LongString] = REPLACE(CAST([LongString] AS nvarchar(max)), @SearchFor, @ReplaceWith) WHERE [LongString] LIKE '%' + @SearchFor + '%'
            PRINT 'Updating soft links'
            UPDATE [tblContentSoftlink] SET [LinkURL] = REPLACE([LinkURL], @SearchFor, @ReplaceWith) WHERE [LinkURL] LIKE '%' + @SearchFor + '%'
      END
Nov 19, 2014

Comments

Nov 19, 2014 02:27 PM

The TSQL above is for EPiServer v7.

For EPiSever v6 the following replacements will need to be made:
- Replace [tblContentProperty] with [tblProperty]
- Replace [tblWorkContentProperty] with [tblWorkProperty]
- Replace [tblContentSoftlink] with [tblPageSoftlink]

Henrik Fransas
Henrik Fransas Nov 19, 2014 03:58 PM

Do not forget that you have to make EPiServer to recycle the cache (IIS reset is the best way)

Nov 19, 2014 04:20 PM

Indeed, our Go pipelines recycle the application pools once content has been refreshed.

Nov 20, 2014 03:17 PM

Nice work Croweman. as ever I'd approach this in a slighlty different way using my favourite tool IIS UrlRewrite:)

Rather than modifying the datbase. I've approached this by adding some outbound rewrite rules. These scan any outbound html from the server and replace all instances of the live url with the test site url (or other environment). The advantage of this per environment configuration approach, is no changes to data. and you can be very sure that no urls matching that pattern willl ever appear, regardless of how they are entered in the website.

Obviously scanning outbound html incurs a small performance hit, but on a test environment this shouldn't be an issue.

I've put a gist together of the type of configuration needed.

https://gist.github.com/markeverard/8aa02cf0539d12cbefd7

Nov 20, 2014 05:37 PM

Very nice Doctor ;)

I didn't realise you could modify outgoing html using the rewrite module :)

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024