Content Search / Replace
Is there a tool or snippet of code available to do a search / replace in all of the generic HTML content entered onto a Sitefinity site? We've got some content to update and it will be far faster in this case if we can do a general search / replace on content entered into the site rather than going through the pages and individually editing them.
Hi Seattle Web Group,
You can use a code similar to this:
var manager = ContentManager.GetManager();
var items = manager.GetContent().Where(c => c.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live);
foreach
(var item
in
items)
Lstring
var content = item.Content.Value;
item.SetValue(
"Content"
, newValue);
manager.SaveChanges();
Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>
The provided code only works for shared content blocks. I am trying to replace the HTML content in all my pages, but not everything is a shared content block. How can I adjust this so it processes on all generic content and not just shared ones?
var contentItem = manager.GetContent().Where(c =>c.Content.Contains(search) && c.Status == ContentLifecycleStatus.Live);
Hi Basem,
We have replied to you in the support thread you had open, for your convenience please find our reply below as well:
"
To get or modify the Content block Html property, you'll need to traverse the controls on your page, get the ones whose ObjectType is ContentBlock, and then obtaint he Html property. An important thing to note is that once you've done modifying the properties of the loaded control, you'll need a call to PageManager.ReadProperties() which will copy the properties of the loaded content block which you've modified to the actual control representation on the page. Please find below a sample code on how you can achieve this:
var pageManager = PageManager.GetManager();
var pageDataList = pageManager.GetPageDataList()
.Where(pD => pD.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live && pD.HtmlTitle ==
"Scheduler"
)
.ToList();
foreach
(PageData pageData
in
pageDataList)
var contentBlocks = editPage.Controls.Where(c => c.ObjectType.StartsWith(
typeof
(ContentBlock).FullName));
foreach
(var contentBlockControl
in
contentBlocks)
ContentBlock contentBlock = pageManager.LoadControl(contentBlockControl)
as
ContentBlock;
contentBlock.Html =
"EDITED from CODE"
;
pageManager.ReadProperties(contentBlock, contentBlockControl);
pageManager.SaveChanges();
pageManager.PublishPageDraft(editPage.Id,
true
);
pageManager.SaveChanges();