Categories
Bloggers
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://screencast.com/t/qUi7DL5AxpI

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">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            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" />
        </div>
    </form>
</body>
</html>

and

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();
                        else
                            pageEdit = pageManager.PagesLifecycle.Edit(page);
                        try
                        {
                            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;
                                            replaceCount++;
                                        }
                                    }
                                    //Save the changes
                                    pageManager.SaveChanges();
                                    //Checking the temp and publish
                                    pageEdit = pageManager.PagesLifecycle.CheckIn(temp);
                                    pageManager.PagesLifecycle.Publish(pageEdit);
                                    pageManager.SaveChanges();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
            }
            return replaceCount;
        }
    }
}
 
You can easily further extend the sample to fit your case.

 

Leave a comment