+1-888-365-2779
Try Now
More in this section
Categories
Bloggers
Blogs RSS feed

ReplaceTool - Globally replace everything in your project with zero efforts

by Georgi Chokov

Have you ever needed to change a single e-mail, located on 2000 pages with an another one? Have you experienced the problem to update 5000 links in your web site? While running some SQL queries against the project database may do the trick, the chance to screw something is pretty big due the number of relations between the tables in the database. That is why we always suggest using the APIs for changing the content items.

In fact we had some problems with the upgrades, to be more specific, in some cases the links to library items, containing whitespaces were broken after upgrading to Sitefinity 3.5. The whitespaces in 3.5 should have been replaced with underscores. But how do you change, let's say 300 images urls, managed in different libraries manually?

Here's a tool that will help you. You can either search and replace terms, or use regular expressions to find content or items you need to change. It is working with our APIs, and it is not compiled so you may take look at its source.

The tool itself is specially prepared for Sitefinity - it is using the ITool interface, which allows you to build components and easily plug them to the Sitefinity CMS. Here is how it looks like:Shot

 

Please follow these steps to include the tool in your project:

1. Download ReplaceTool

Here are the steps you should follow in order to enable ReplaceTool in your project:

2. Extract [Archive]\App_Code\ReplaceTool.cs file to \[YourProject]\App_Code directory

3. Extract [Archive]\Tools directory to your \[YourProject]\Tools directory. If you do not have Tools directory, please create it.

4. Open your web.config file, and add the line to the <tools> node:

<tools> 
... 
<add type="Telerik.Cms.Tools.ReplaceTool, App_Code" /> 
</tools> 

5. Give the asp.net worker process read access to the newly created files (steps 1 and 2).

6. Run the project and browse to Administration->Tools->ReplaceTool

 

Please note that there is no "Undo" function yet. This means that you should backup your database in case you want to give it a try. The tool can be used only by users in the Administrators role (don't ask me why :) )

 

I hope it would also be helpful for you, if you want to see how to change pages, content items and control properties programmatically.

Let me know if you have any questions on the matter.

21 comments

Leave a comment
  1. Romi Dec 17, 2008
    Hi,
    Many thanks Georgi for this usefull tool.
  2. Marko Dec 17, 2008
    Yes, thanks for the tool!  I was talking with Ivan previously and he was helping me fix all of the broken links that before ver 3.5 used to be allowed to include a whitespace.  So we had ended up with a lot of hardcoded (contextual) links to .sflb files that needed to be updated.  For example, a link in GeneralContent control that has href="/Libraries/MyLib/My File.sflb" needed to be changed into href="/Libraries/MyLib/MyFile.sflb", and so on.  I ended up using this tool to do the actual search and replace.  But it took me a while to figure out the correct regex pattern, especially because I suck at regex.

    So, if anyone cares, this seems to have done the job properly, covering any case I could throw at it:

    (?<=href="[^"]*)\s(?=[^"]*sflb[^"]*")
  3. Marko Dec 17, 2008
    ^
    Correction above: a link such as href="/Libraries/MyLib/My File.sflb" needed to be fixed into href="/Libraries/MyLib/My_File.sflb".  Also forgot to mention that when using regex search pattern I specified above, you ONLY need to use a _ (a single underscore) in the "replace with" box, because the regex search pattern only captures the desired whitespaces.
  4. Gabe Sumner Dec 17, 2008
    This tool is awesome!  As Georgi warned, backup your databases and be careful with your replacement patterns.  :)
  5. Georgi Dec 19, 2008
    I am really glad you like it!
    Marko, we can use this regex :) Thanks!
  6. Nick Jan 07, 2009
    Can this tool be used with version 3.2?
  7. Georgi Jan 08, 2009
    Nick,
    Yes, this should work because the APIs are backward compatible.
  8. Paul Apr 07, 2009
    Gave me a great insight into Sitefinity's page handling.

    However, how can you manage to access the list items' values, when parsing the CheckBoxList:

     foreach (ListItem li in providersList.Items)
            {
                if (li.Selected)
                {
                    // for each selected provider content and meta field values will be scanned and
                    // search term will be replaced with replace term
     ==>>      if (li.Value.Equals("Lists"))
                    {

    Asp.net drops checkbox values, and as far as I know it should work this way:

    Checkbox li = new Checkbox();
    [...]
    li.InputAttributes.Add("value", providerName);

    And in the end you'll have:

     foreach (ListItem li in providersList.Items)
            {
                if (li.Checked)
                {

                if (li.InputAttributes["value"] == Lists)...

    Or am I wrong?

    Because if I choose to get the listitem value I receive a NullException

  9. sparker Apr 28, 2009
    i ended up using a couple regex very close to what Marko created. some of my links and images had %20 instead of spaces and they had a extension of .sflb and not the new .sflb.ashx (this can be changed to anything but may be helpful)

    * replace spaces or %20 in image src
    (?<=src="[^"]*)( |%20)(?=[^"]*sflb[^"]*")
    replace with _

    * change the file extension for images
    (?<=src="[^"]*).sflb(?=")
    replace with .sflb.ashx

    * replace spaces or %20 in links
    (?<=href="[^"]*)( |%20)(?=[^"]*sflb[^"]*")
    replace with _

    * change the file extension for links
    (?<=href="[^"]*).sflb(?=")
    replace with .sflb.ashx
  10. Bart Sep 30, 2009
    Will this tool show you the items before replacing them?  Just in case you need to keep certain instance of the expression the same and not changed.
  11. fregas Jan 28, 2010
    Can someone explain what the "=" is before the src or href attributes?  Also, is there a way to remove parenthesis from the filename?
  12. Rob Feb 18, 2011
    Saved me hours of work!

    Thank you so much.
  13. Vangelis Apr 27, 2011
    Very helpful. In case that you have used ( or ) in any of your images description then you have to run

    * replace ( or %28 in image src
    (?<=src="[^"]*)( |%28)(?=[^"]*sflb[^"]*")
    replace with _

    * replace ( or %29 in image src
    (?<=src="[^"]*)( |%28)(?=[^"]*sflb[^"]*")
    replace with any other character than _

    if _ is the last character before extention ex image_..sflb.ashx then it displays the image as broken link
  14. Vangelis Apr 27, 2011
    correct one is the below for right )

    * replace ) or %29 in image src
    (?<=src="[^"]*)( |%29)(?=[^"]*sflb[^"]*")
  15. John Jul 12, 2011
    Does this tool work in V4.1?
    If so could you give me some pointers on how to install it, as I can't find the <tools> section in the web.config, and am not sure where to add it.

    Any suggestions would be great, as this looks like a fantastic tool.
  16. Vagelis Oct 07, 2011

    Server Error in '/' Application.

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 241:
    Line 242:            // replace the search phrase with replace phrase in content
    Line 243:            string stringContent = cntInTransaction.Content as string;
    Line 244:            if (stringContent != null)
    Line 245:            {

    Source File: c:\telerik\Sitefinity3.7\WebSites\test32patch37\Tools\ReplaceTool\ReplaceTool.ascx.cs    Line: 243

    Stack Trace:

    [NullReferenceException: Object reference not set to an instance of an object.]
       Tools_ReplaceTool_ReplaceTool.ReplacePhraseInContentItem(String providerName) in c:\telerik\Sitefinity3.7\WebSites\test32patch37\Tools\ReplaceTool\ReplaceTool.ascx.cs:243
       Tools_ReplaceTool_ReplaceTool.replaceButton_Click(Object sender, EventArgs e) in c:\telerik\Sitefinity3.7\WebSites\test32patch37\Tools\ReplaceTool\ReplaceTool.ascx.cs:62
       System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +111
       System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +79
       System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +175
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
    


    Version Information: Microsoft .NET Framework Version:2.0.50727.5446; ASP.NET Version:2.0.50727.5420              
  17. Sharon Feb 16, 2012
    This tool is a huge help! I used it with 3.7 sp4 to remove a dash from 3 of our product names - it would have taken forever manually.

    The only part that didn't want to work right was the page title option but that left me just a handful of changes.

    Thanks for writing this!
  18. Chris Apr 13, 2012
    Is there a version or supported built in tool similar to this for 4.3?
  19. jason Apr 19, 2012
    Hi,

    Does this work in version 5?  It would be a huge help as we have quite a bit of changes.

    Thanks
  20. Matt May 03, 2012
    Georgi, you are the man! This tool just completely saved my life on a big 3.2 -> 5.0 upgrade/migration! Thank you!
  21. Tejal Satre Jun 23, 2014

    Hi,

    Is this tool compatible with sitefinity 7.0??

    Thanks

    Leave a comment