Feedback
Did this article resolve your question/issue?

   

Article

How to replace or change the text of the same content block which is added in multiple pages

« Go Back

Information

 
TitleHow to replace or change the text of the same content block which is added in multiple pages
URL NameHow-to-replace-or-change-the-text-of-the-same-content-block-which-is-added-in-multiple-pages
Article Number000112738
EnvironmentProduct: Sitefinity
Version: 7.x, 8.x. 9.x, 10.x, 11.x, 12.x, 13.x
OS: All supported OS versions
Database: All supported Microsoft SQL Server versions.
Question/Problem Description
How to replace or change the text of the same content block in multiple pages?
How to modify the content of a content block located on a specific page?
How to update the content of a content block placed in a page template?
 
Steps to Reproduce
Clarifying Information
Error Message
Defect Number
Enhancement Number
Cause
Resolution
For pages:
private void UpdatePage()
{
    PageManager pageManager = PageManager.GetManager();

    var pageData = pageManager.GetPageData(id);
    var draftPage = pageManager.EditPage(pageData.Id);
    var contentBlockDraftControls = draftPage.Controls.Where(c => c.Caption == "Content block");

    foreach (var contentBlockDraftControl in contentBlockDraftControls)
    {
        if (contentBlockDraftControl != null)
        {
            // For WebForms based content block
            var wfContentBlock = pageManager.LoadControl(contentBlockDraftControl) as Telerik.Sitefinity.Modules.GenericContent.Web.UI.ContentBlock;
            if (wfContentBlock != null)
            {
                wfContentBlock.Html = wfContentBlock.Html.Replace("old value", "new value");
                pageManager.ReadProperties(wfContentBlock, contentBlockDraftControl);
            }

            // For MVC based content block
            var mvcContentBlock = pageManager.LoadControl(contentBlockDraftControl) as MvcControllerProxy;
            if (mvcContentBlock != null)
            {
                var settings = mvcContentBlock.Settings as ControllerSettings;

                if(settings != null)
                {
                    object content;
                    settings.Values.TryGetValue("Content", out content);

                    if(content != null)
                    {
                        var updatedContent = content.ToString().Replace("old value", "new value");
                        settings.Values.Remove("Content");
                        settings.Values.Add("Content", updatedContent);
                        pageManager.ReadProperties(mvcContentBlock, contentBlockDraftControl);
                    }
                }
            }
        }
    }

    pageManager.PublishPageDraft(draftPage);
    pageManager.SaveChanges();    
}

For page templates:
private void UpdatePageTemplate()
{
    PageManager pageManager = PageManager.GetManager();

    var pageTemplate = pageManager.GetTemplate(id);
    var draftTemplate = pageManager.EditTemplate(pageTemplate.Id);
    var contentBlockDraftControls = draftTemplate.Controls.Where(c => c.Caption == "Content block");

    foreach (var contentBlockDraftControl in contentBlockDraftControls)
    {
        if (contentBlockDraftControl != null)
        {
            // For WebForms based content block
            var wfContentBlock = pageManager.LoadControl(contentBlockDraftControl) as Telerik.Sitefinity.Modules.GenericContent.Web.UI.ContentBlock;
            if (wfContentBlock != null)
            {
                wfContentBlock.Html = wfContentBlock.Html.Replace("old value", "new value");
                pageManager.ReadProperties(wfContentBlock, contentBlockDraftControl);
            }

            // For MVC based content block
            var mvcContentBlock = pageManager.LoadControl(contentBlockDraftControl) as MvcControllerProxy;
            if (mvcContentBlock != null)
            {
                var settings = mvcContentBlock.Settings as ControllerSettings;

                if(settings != null)
                {
                    object content;
                    settings.Values.TryGetValue("Content", out content);

                    if(content != null)
                    {
                        var updatedContent = content.ToString().Replace("old value", "new value");
                        settings.Values.Remove("Content");
                        settings.Values.Add("Content", updatedContent);
                        pageManager.ReadProperties(mvcContentBlock, contentBlockDraftControl);
                    }
                }
            }
        }
    }

    pageManager.PublishTemplateDraft(draftTemplate);
    pageManager.SaveChanges();    
}
Workaround
Notes
Last Modified Date7/23/2020 3:27 PM
Attachment 
Files
Disclaimer The origins of the information on this site may be internal or external to Progress Software Corporation (“Progress”). Progress Software Corporation makes all reasonable efforts to verify this information. However, the information provided is for your information only. Progress Software Corporation makes no explicit or implied claims to the validity of this information.

Any sample code provided on this site is not supported under any Progress support program or service. The sample code is provided on an "AS IS" basis. Progress makes no warranties, express or implied, and disclaims all implied warranties including, without limitation, the implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample code is borne by the user. In no event shall Progress, its employees, or anyone else involved in the creation, production, or delivery of the code be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample code, even if Progress has been advised of the possibility of such damages.