More in this section
Blogs RSS feed

Global Search and Replace in Sitefinity Content Blocks

by Atanas Valchev

Sitefinity offers Shared Content Block, which allows static content on multiple pages to be easily managed from one place, eliminating the need of going through every page and editing it. However, in some cases, there are content blocks that are not shared and we would like to easily replace content on them.

This can be done by going through all pages, finding the content blocks and doing a search and replace in the html. Here is a video of the end result: ttp://

Here is the full code for the sample:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SearchAndReplace.aspx.cs" Inherits="SitefinityWebApp.SearchAndReplace" %>
<!DOCTYPE html>
<head runat="server">
    <form id="form1" runat="server">
            culture:<asp:TextBox id="cultureTxtBox" runat="server" />
            Search:<asp:TextBox ID="srchContentTxtxBox" runat="server" />
            Replace:<asp:TextBox ID="replaceContentTxtxBox" runat="server"></asp:TextBox>
            Do:<asp:Button ID="srchAndReplaceBtn" Text="Search and replace" runat="server" />
            Done:<asp:Literal ID="statusLiteral" runat="server" />


using System;
using System.Globalization;
using System.Linq;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Modules.GenericContent.Web.UI;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Pages.Model;
namespace SitefinityWebApp
    public partial class SearchAndReplace : System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
            srchAndReplaceBtn.Click += srchAndReplaceBtn_Click;
        void srchAndReplaceBtn_Click(object sender, EventArgs e)
            var replacedInstances = GlobalSearchAndReplace(srchContentTxtxBox.Text, replaceContentTxtxBox.Text, this.cultureTxtBox.Text);
            this.statusLiteral.Text = String.Format("{0} ocurrences replaced", replacedInstances);
        public int GlobalSearchAndReplace(string searchString, string replaceString, string cultureName="en")
            var replaceCount = 0;
            var pageManager = PageManager.GetManager();
            var pageNodes = pageManager.GetPageNodes().Where(p => p.RootNodeId == SiteInitializer.FrontendRootNodeId);
            foreach (var pageNode in pageNodes)
                // var pageNode = pageManager.GetPageNodes().Where(p => p.Title == "imageullsize").FirstOrDefault();
                PageDraft temp;
                PageDraft pageEdit;
                if (pageNode != null)
                    var page = pageNode.Page;
                    if (page != null)
                        var temps = page.Drafts.Where(d => d.IsTempDraft == true).ToList();
                        if (temps.Count > 1)
                            pageEdit = temps.FirstOrDefault();
                            pageEdit = pageManager.PagesLifecycle.Edit(page);
                            temp = pageManager.PagesLifecycle.CheckOut(pageEdit);
                            var culture = CultureInfo.GetCultureInfo(cultureName);
                            foreach (var control in temp.Controls)
                                if (control.ObjectType == typeof(ContentBlock).FullName)
                                    //get the control HTML property from the database
                                    var htmlProperty = control.GetProperties(culture, false).Where(p => p.Name == "Html").SingleOrDefault();
                                    if (htmlProperty != null)
                                        //get the control HTML
                                        var contentBlockHtml = htmlProperty.Value;
                                        ////update the HTML property in the database
                                        if (contentBlockHtml.ToString().Contains(searchString))
                                            var newHtml = contentBlockHtml.ToString().Replace(searchString, replaceString);
                                            htmlProperty.Value = newHtml;
                                            htmlProperty.MultilingualValue = newHtml;
                                    //Save the changes
                                    //Checking the temp and publish
                                    pageEdit = pageManager.PagesLifecycle.CheckIn(temp);
                        catch (Exception ex)
            return replaceCount;
You can easily further extend the sample to fit your case.


1 comment

Leave a comment
  1. Carl Mar 01, 2016

    Would I be able to use this tool to replace HTML code as well?


    Leave a comment