Blog: Umbraco Hidden Folders
At Moriyama, we support Umbraco sites running many different versions - from v6 all the way through to v8. Our support allows customers to request small new features or tweaks to their site, as we all as the more critical support requests that happen from time to time.
A recent request was to help a customer reorganise some of their content into folders. It was a large mature site with thousands of pages. However, over time, scant regard had been paid to the structure of the site - with many content and landing pages being created at the root of the site.
This had led to a sub-optimal editor experience, as finding pages was becoming harder and harder. So they asked us to give them the ability to create folders, into which they could organise this legion of landing pages. However, and here’s the tricky bit, they didn’t want the URL to reflect the folder structure.
For example, if they had a page at the URL /prepare-for-brexit/ they wanted to be able to put it in a folder called ‘brexit’ without the URL becoming:/brexit/prepare-for-brexit/ - which is the default Umbraco behaviour.
They also wanted the solution to be intuitive for editors and not introduce redirects or entry of alternative URLs. Therefore, we used a Hidden Folder technique which allowed them to create folders without affecting the URL structure.
If your editors are itching to give their content tree a spring clean, then follow our recipe below to enable hidden folders - before you know it the content tree will be a cleaner more pleasant place to be and your editors will be cock-a-hoop (Possibly an overstatement .ed).
For this Hidden Folder to work, we needed to create the following:
- Hidden Folder document type
- UrlProvider to give us the correct URL.
- ContentFinder for Hidden Folders
- ContentFinder for children of Hidden Folders
- Register our content finders and UrlProvider
Hidden Folder document type
To start with we create a new document type, without a template, called Hidden Folder giving it an alias of umbracoHiddenFolder.
Next, we need to edit the permissions of this document type, this controls which document types can be created below it. i.e. Standard Content
Now that we have created the document type, Save it and edit the permissions of the other doc types so these folders can be created under them.
Our next move is to create a test Hidden Folder under the home node called Test Folder. When we save and publish it, we can see that it has a URL of /test-folder/, as per the default Umbraco URL naming system.
If we create a content page under this folder called Test Content Page. When we save and publish it, the URL of the content page will be /test-folder/test-content-page/ but, of course, we want it to be /test-content-page/. So what next?
We need to write some code to make this work
The first thing we need to do is create a UrlProvider. This UrlProvider will take care of the Hidden Folders and any items under them. This won’t work on its own though, we will need to implement the content finders too.
We are going to override the GetUrls method and put our own logic in there.
First, we need to find out if the content item is a Hidden Folder, or if it has an ancestor which is a Hidden Folder.
An example piece of code is here.
Content Finders
We also need to create a content finder which inherits from ContentFinderByNiceUrl
We will override the TryFindContent method and use an XPath expression to find the content item as a descendant of a Hidden Folder. This means it will still find it if our structure is like this:
- Home
- Hidden Folder
- Hidden Folder
- Content Page (this will be found)
or this:
- Home
- Hidden Folder
- Content Page (this will be found)
N.B It doesn’t work for content items which are not direct children of Hidden Folders though, like this:
- Home
- Hidden Folder
- Hidden Folder
- Content Page (this will be found)
- Content Page (this won’t be found)
Read through the comments to understand what each section is doing and why.
Now that we have a content finder which finds the content under the Hidden Folders, we need to create one which will find the Hidden Folders. Read through the comments to understand what each section is doing and why.
Registering the UrlProvider and ContentFinders
To Register the UrlProvider and ContentFinders we need to hook into the ApplicationStarting event and add them in. Notice that we take out the default ContentFinderByNiceUrl because we inherit from that in ours and we call it first.
With this simple code in place, your new Hidden Folders will be working and you can freely create pages inside them with URLs that give nothing away about the folders they are under.
Final Note
Don’t forget to cater for these Hidden Folders in your breadcrumb and on your sitemap! You probably don’t want to write out the links to these folders, but you may still want to write out the links for their contents.
Can you help me set this up?
Yes! If you are a Moriyama support customer we can do this for you, or if not, use the form below to find out how, you too can benefit from our knowledgable, friendly and pro-active support team.