Finding a specific text-string in your Umbraco content

18 januari 2016  |  Gerben van Diggelen

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 a go with a problem. Some time ago she upload a file to the Umbraco media section, and subsequently added a few links to the file in different content nodes. A few month 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 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 was added. You can use this tool to find a certain string in any propery 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 a 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 a 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.

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

Gerelateerde blogs

Geinteresseerd geraakt? neem contact op