Gerben van Diggelen
18-1-2016

Finding a specific text-string in your umbraco content

Sometimes you need to find a certain string somewhere in your Umbraco website. I made a tool that makes the job much easier. 

 

The scenario

A client approached me a while ago with a problem. Some time ago she uploaded a file to the Umbraco media section, and subsequently added a few links to the file in different content nodes. A few months later she updated the file in the media section with another file, which had a different file-name. Due to this bug (or usability problem) in Umbraco she had to change the links to the media file in all the content nodes. Unfortunately you cannot use the default Umbraco search because it does not search in the html.

 

The solution

This is why I created a tool to help the client find all pages where the link is added. You can use this tool to find a certain string in any property on any document type. I created a package which you can download here for easy use. The tool adds a macro which you can add to any RTE or view. It is a simple form with an input field and a submit button. The tool puts out a list of found notes with a link to the page and a link to the node in Umbraco.

How to use

Install the package in umbraco via the interface. It adds a macro that u can use inline in any rich text editor.

 

When to use

You can use this tool to search for specific strings like urls or macro's. This is not something you want your visitors to use, there are better tools for that.

 

How it works

The bulk of the work is done by one foreach statement.

@foreach (var node in Umbraco.TypedContentAtRoot().SelectMany(r => r.Descendants().Where(x => x.Properties.Any(y => y.DataValue != null && y.DataValue.ToString().Contains(q)))))

Foreach root(r) node in the content tree we retrieve the descendants. Then for every descendant we need to check if any property contains the string(x). x.Properties returns an ICollection with all properties of the node so we can check if any property contains the string(q). It is important to check the DataValue in the property and not the value. Value compiles the macro's added in rich text editors.

 

Note

This tool uses published content only. You can use ContentService if you want to include unpublished content but that is a lot slower.

Meer weten?
Neem contact op met Gerben.

Gerben van Diggelen
Operationeel directeur
[email protected]