Benjamin Tinker Team : Web Development Tags : Web Development

Umbraco – Utilising uComponents for simpler search expressions

Benjamin Tinker Team : Web Development Tags : Web Development

One of the good things I have come across while developing within the Umbraco environment was the uComponents add-on. Among its many useful tools for new data types, keyboard short-cuts, drag-n-drop functionality and all the bells and whistles of developer tool add-ons is the ability to greatly increase the efficiency of your searches. Umbraco’s native searches usually take the form of creating a DocumentType object  and then using that object to search for a Document within a node structure. You can then use some Linq queries to gather information of the Documents you are searching for.

The code below does a simple search of a document tree for a node that contains a property with a set value.

DocumentType dt = DocumentType.GetByAlias('myDocType');

List docs = Document.GetDocumentsOfDocumentType(dt.Id).Where(x => x.GetProperty(“myProperty”).Value == “ValueToSearchFor”).ToList());

This works by returning a list of all the Documents of type myDocType that have a property myProperty that equals ValueToSearchFor. We have then cast it to a list to do whatever comes next.

The downfall of this is when you want to perform searches that are either for a date and numerical values. Umbracos native searching is limited in that you have to perform the x.GetProperty(“myProperty”).Value expression each time to gather a value. And this is done before you can determine the type of the property. Using this method will always return the value as a string which is of no help for doing date and numerical ranges. How do we get around this?

Bring on uComponents!

By adding the uComponents add-on to your site you open up a whole range of functions that allow for ease of searching as one. There is just not enough space to go over them all here so we’ll just look at the GetPropertyAsString, GetPropertyAsDateTime and GetPropertyAsInt extensions.

Once you have added uComponents to your site you can then take advantage of its libraries in development and the front-end of your site. Firstly we include the core components on our webpage for the searches.

using uComponents.Core;

using uComponents.Core.uQueryExtensions;

These open up the box for your power.

We can then utilise the searches to have some greater searching power.

GetPropertyAsString can be used to always return a value of a node as a string. We can change our query above to the following to return the same results. Note that we do not have to explicitly query the Value of the property to do any comparisons against it.

DocumentType dt = DocumentType.GetByAlias('myDocType');

List docs = Document.GetDocumentsOfDocumentType(dt.Id).Where(x => x. GetPropertyAsString(“myProperty”)== “ValueToSearchFor”).ToList());

Now if we want to search for a date and/or numerical value we can use the GetPropertyAsDateTime and GetPropertyAsInt extensions for some easier searching. For example we can update our new query now to only return nodes that were created after a certain date and have been viewed more than 100 times.

DocumentType dt = DocumentType.GetByAlias('myDocType');

List docs = Document.GetDocumentsOfDocumentType(dt.Id).Where(x => x. GetPropertyAsString(“myProperty”)== “ValueToSearchFor” && x. GetPropertyAsDateTime(“created”) > myStartDate && x. GetPropertyAsInt(“viewCounter”)>100 ).ToList());

We now have the ability to search for nodes which much more specific criteria without the need to do post query parsing and data type checking.  If necessary you can check for null values and even select specific node properties with Linq knowing they will be automatically cast into the correct type.

By building on these extensions developers are now able to create much more efficient search queries that get the information required in one sweep. And what developer does not like having to write less?