Configuration vs Code – it’s a classic dilemma. Both have their ups and downs. However, I am the kind of guy that tends to prefer code over configuration (you already guessed, I’m sure). Today I got a question if it’s possible to attach a page provider programmatically – and of course it is. Actually, all you have to do is to instantiate your provider, initialize it and add it to the PageProvider Map in the DataFactory.
If you want to persist over IIS restarts, however you also need to add it to your configuration (in the EPiServerSection.Instance.PageProvider.Providers collection).
I made a very basic example of how to do this. As an example I am using an experimental provider that I haven’t blogged about yet – and which is still a little bit buggy – but it gets the job done :-) I call it the FPWCProvider. Yes…FPWC as in that hated FindPagesWithCriteria method. The idea is that it’s a provider that basically performs an FPWC search and returns the results on it’s first level. So if you for instance want to have a section of your site return a list of the latest press-releases from all over your site, you can simply configure it to search for all Press Release pagetypes and list them there.
In order to attach it on the fly I do a bit of a hack – I have created a PageType called FPWCProvider and whenever people try to create a page of that type, it won’t be created. Instead it’ll attach the page provider to that location. As an example, trying to publish this:
The code needed to attach this:
Ok, ok. I bet you are also wondering on the logic of the FPWC provider – so even though it’s unfinished and not really relevant, here you go: